首页 > 代码库 > 原 Java学习之Xml系列二:xml按条件查询、xml递归遍历所有元素和属性
原 Java学习之Xml系列二:xml按条件查询、xml递归遍历所有元素和属性
xml中加入了几条,为了方便查询时作为示例。
话不多说见代码注释:
DTD文件:SwordTypeDefinition.dtd
<?xml version="1.0" encoding="UTF-8"?> <!ELEMENT SwordLibrary (Sword*)> <!ELEMENT Sword (SwordName,Price,Attack)> <!ELEMENT SwordName (#PCDATA)> <!ELEMENT Price (#PCDATA)> <!ELEMENT Attack (#PCDATA)> <!ATTLIST Sword sno CDATA #REQUIRED> <!ATTLIST Price type CDATA #IMPLIED> <!ATTLIST Attack factor CDATA "1.0">
XML文件:SwordLib.xml
<?xml version="1.0" encoding="UTF-8" standalone="no"?> <!DOCTYPE SwordLibrary SYSTEM "SwordTypeDefinition.dtd"> <SwordLibrary> <Sword sno=‘s1‘> <SwordName>欢欣之刃</SwordName> <Price>1000</Price> <Attack>10</Attack> </Sword> <Sword sno=‘s2‘> <SwordName>夜叉</SwordName> <Price>2050</Price> <Attack factor="2.0">30</Attack> </Sword> <Sword sno=‘s3‘> <SwordName>魔棒</SwordName> <Price type="Dollar">200</Price> <Attack>0</Attack> </Sword> </SwordLibrary>
java代码:
package JavaLeaner.XmlTest; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.parsers.ParserConfigurationException; import org.junit.Test; import org.w3c.dom.Document; import org.w3c.dom.Element; import org.w3c.dom.NamedNodeMap; import org.w3c.dom.NodeList; import org.xml.sax.SAXException; public class XmlDemo2 { /* * 按照属性sno查询 */ @Test public void Test1() throws IOException, ParserConfigurationException, SAXException { System.out.println("请输入查找的sword的sno:"); //这里是java 的控制台输入方法,老忘记,TT BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); String sno=br.readLine(); Element st= FindSwordBySno(sno); if (st != null) { String sname = st.getElementsByTagName("SwordName").item(0).getTextContent(); System.out.println("此剑为:" + sname); } else { System.out.println("这里不卖!!" ); } /* 请输入查找的sword的sno: s2 此剑为:夜叉 */ } Element FindSwordBySno(String sno)throws ParserConfigurationException, SAXException, IOException { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilder docDuilder = factory.newDocumentBuilder(); Document doc = docDuilder.parse("src/JavaLeaner/XmlTest/SwordLib.xml"); NodeList list = doc.getElementsByTagName("Sword"); for(int i=0;i<list.getLength();i++) { Element swordTag=(Element)list.item(i); String snoText=swordTag.getAttribute("sno"); if(snoText.equals(sno)) { return swordTag; } } return null; } /* * 递归遍历整个xml文档的元素和属性 */ @Test public void Test2() throws IOException, ParserConfigurationException, SAXException { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilder docDuilder = factory.newDocumentBuilder(); Document doc = docDuilder.parse("src/JavaLeaner/XmlTest/SwordLib.xml"); //取文档根元素 Element rootElement= doc.getDocumentElement(); String rootName = rootElement.getTagName(); System.out.println(rootName); DeepIn(rootElement); } void DeepIn(Element parentElement) { NodeList list=parentElement.getChildNodes(); for(int i=0;i<list.getLength();i++) { if (list.item(i) instanceof Element) { Element nodeElement = (Element) list.item(i); String eName = nodeElement.getNodeName(); System.out.println(eName); NamedNodeMap nnm=nodeElement.getAttributes(); //注意:NamedNodeMap也不支持java.lang.Iterable,所以不能增强佛如循环 for(int j=0;j<nnm.getLength();j++) { String aName = nnm.item(j).getNodeName(); String aText = nnm.item(j).getTextContent(); System.out.println(" "+aName+"="+aText); } DeepIn(nodeElement); } else { //System.out.println("not Element:"+list.item(i).getTextContent()+"------"); /* 注意: getChildNodes()获取的不仅仅包括子元素,还包括其他的字符串等文本,这里之所以会出现这些not Element:-----,是因为xml文件中有许多空白符和换行的缘故 * 在实际使用中,可以像本例一样用instanceof Element的条件判断将这些东西过滤掉。 * 这个结果不包含属性部分的代码: * SwordLibrary not Element: ------ Sword not Element: ------ SwordName not Element:欢欣之刃------ not Element: ------ Price not Element:1000------ not Element: ------ Attack not Element:10------ not Element: ------ not Element: ------ Sword not Element: ------ SwordName not Element:夜叉------ not Element: ------ Price not Element:2050------ not Element: ------ Attack not Element:30------ not Element: ------ not Element: ------ Sword not Element: ------ SwordName not Element:魔棒------ not Element: ------ Price not Element:200------ not Element: ------ Attack not Element:0------ not Element: ------ not Element: ------*/ } } /* 结果: * * SwordLibrary Sword sno=s1 SwordName Price Attack factor=1.0 Sword sno=s2 SwordName Price Attack factor=2.0 Sword sno=s3 SwordName Price type=Dollar Attack factor=1.0*/ } }
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。