解析XML文件的方法
1SAX解析 1 1-1外部配置 1 1-2具体的实现 1 1-3效果图 3 2DOM解析(DocumentobjectModel)w3c 3 2-1用到的函数解析 3 Document: 3 Element: 7 2-2实例: 8 2-2-1parse1 8 2-2-1parse2 9 2-2-3getNodes获取节点 10 2-2-4测试 10 3JDOM解析 导入jdom包 11 4DOM4J解析导入的dom4j架包 11 4-1parse1 11 4-2parse2 12 4-3测试 13
1SAX解析解析出的和原来的配置文件几乎一样 1-1外部配置(1)需要继承DefaultHandler (2)重写五个方法 Characters:标签中的文本值 endDocument:结束文档 endElement:结束标签 startDocument:文档开始解析 startElement:开始标签属性包含在开始标签里面 1-2具体的实现publicclassParseBySAXextendsDefaultHandler{ //标签中的文本值 @Override voidcharacters(char[]ch,intstart,85)">intlength) throwsSAXException{ System.out.print(newString(ch,start,length)); } //结束文档 voidendDocument()out.print("文档结束解析"); } //结束标签 voidendElement(Stringuri,StringlocalName,StringqName) "<"+qName+">"); } voidstartDocument()out.println("文档开始解析"); } //开始标签属性包含在开始标签里面 voidstartElement(Stringuri,StringqName, Attributesattributes)throwsSAXException{ Strings="<"+qName; for(inti=0;i<attributes.getLength();i++){ StringattrName=attributes.getQName(i); StringattrValue=attributes.getValue(i); s+=""+attrName+"=""+attrValue+"""; } s+=">"; System.out.print(s); } staticvoidmain(String[]args)throwsException,SAXException{ //创建解析工厂 SAXParserFactoryfactory=SAXParserFactory.newInstance(); //获取解析器 SAXParserparse=factory.newSAXParser(); //开始解析 parse.parse(newFile("src/book.xml"),85)">newParseBySAX()); } } 1-3效果图文档开始解析 <books> <bookno="01011"> <name>还珠格格<name> <author>琼瑶<author> <publishid="1">湖南电视台<publish> <price>3<price> <book> <bookno="01022"> <name>神雕侠侣<name> <author>包中<author> <publish>湖南电视台<publish> <price>22<price> <book> <bookno="01033"> <name>一代枭雄<name> <author>孙红雷<author> <publish>山地<publish> <price>123<price> <book> <books>文档结束解析 2DOM解析(DocumentobjectModel)w3c2-1用到的函数解析Document:getDocumentElement():该属性允许直接访问文档的文档元素的子节点 getElementsByTagName(name):按文档顺序返回包含在文档中且具有给定标记名称的所有Element的NodeList。 Element:2-2实例:2-2-1parse1voidparse1(Documentdocument){ //获取文档的根元素 Elementroot=document.getDocumentElement(); System."获取文件的根元素"+root.getNodeName()); //获取根元素的所有子元素 NodeListnodes=root.getChildNodes(); System."获取根元素的所有子元素的个数"+nodes.getLength()); inti=0;i<nodes.getLength();i++){ Nodenode=nodes.item(i); //System.out.println(node.getNodeType()); //判断当前节点是否是一个完整的标签元素 if(node.getNodeType()==Node.ELEMENT_NODE){ //获取当前节点所有属性的集合 NamedNodeMapmap=node.getAttributes(); Noden=map.getNamedItem("no"); System."n0:"+n.getNodeValue()); //获取book节点下的所有子节点 NodeListlist=node.getChildNodes(); intj=0;j<list.getLength();j++){ Noden2=list.item(j); if(n2.getNodeType()==Node.//获得节点的内容 Stringtext=n2.getTextContent(); System.out.println(n2.getNodeName()+":"+text); } } System.out.println(); } } } 2-2-1parse2voidparse2(Documentdocument){ NodeListlist=document.getElementsByTagName("book"); inti=0;i<list.getLength();i++){ Nodenode=list.item(i); Stringno=node.getAttributes().getNamedItem("no").getNodeValue(); System."no:"+no); NodeListnodes=node.getChildNodes(); intj=0;j<nodes.getLength();j++){ if(nodes.item(j).getNodeType()==Node.ELEMENT_NODE){ System.out.println(nodes.item(j).getNodeName()+":"+nodes.item(j).getTextContent()); } } System.out.println(); } } 2-2-3getNodes获取节点voidgetNodes(Noden){ if(n.hasChildNodes()){ NodeListn1=n.getChildNodes(); inti=0;i<n1.getLength();i++){ Nodenode=n1.item(i); out.println(node.getNodeName()+":"+node.getTextContent()); } getNodes(node); } } } 2-2-4测试voidmain(String[]args){ try{ //获得解析器工厂用来生产解析器 DocumentBuilderFactoryfactory=DocumentBuilderFactory.newInstance(); //获得解析器 DocumentBuilderbuilder=factory.newDocumentBuilder(); //解析指定源获得解析文档 Documentdocument=builder.parse("src/book.xml")); System.out.println(document); //newParseByDom().parse1(document); //newParseByDom().parse2(document); Noden=document.getDocumentElement(); newParseByDom().getNodes(n); }catch(Exceptione){ e.printStackTrace(); } } 3JDOM解析 导入jdom包//获得解析器 SAXBuilderbulider=newSAXBuilder(); //解析文件获取文档对象 Documentdocument=bulider.build("src/book.xml")); //获取文档根元素 Elementroot=document.getRootElement(); //获取指定所有的book节点 Listlist=root.getChildren(for(Objectobj:list){ Elementele=(Element)obj; //获取当前节点中指定属性的值 Stringno=ele.getAttributeValue("no"); Stringname=ele.getChild("name").getText(); Stringauthor=ele.getChild("author").getText(); Stringpublish=ele.getChild("name").getText(); Stringprice=ele.getChild("price").getText(); System."no:"+no); System."name:"+name); System."author:"+author); System."publish:"+publish); System."price:"+price); System.out.println(); } 4DOM4J解析导入的dom4j架包4-1parse1voidparse1(Documentdocument){ Elementroot=document.getRootElement(); //获取当前根元素下所有的book子元素 Iterator<Element>it=root.elementIterator(while(it.hasNext()){ Elemente=it.next(); //获取属性 Stringno=e.attributeValue("no"); //获取子节点 Stringname=e.element("name").getTextTrim(); Stringauthor=e.element("author").getTextTrim(); Stringpublish=e.element("publish").getTextTrim(); Stringprice=e.element("price").getTextTrim(); System.out.println(); } } 4-2parse2voidparse2(Documentdocument){ //找寻大节点里面有小的节点 List<Node>list=document.selectNodes("books/book"); for(Noden:list){ Stringno=n.valueOf("@no"); //获得单个的 Stringname=n.selectSingleNode("name").getName().trim(); Stringauthor=n.selectSingleNode("author").getName().trim(); Stringpublish=n.selectSingleNode("publish").getName().trim(); Stringprice=n.selectSingleNode("price").getName().trim(); System.out.println(); } } 4-3测试throwsException{ //获取解析器 SAXReaderreader=newSAXReader(); //开始解析 Documentdocument=reader.read(//newParseByDOM4J().parse1(document); newParseByDOM4J().parse1(document); } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |