java 递归遍历对象所有属性_Java学习之Xml系列二:xml按条件查询、xml递归遍历所有元素和属性...
xml中加入了幾條,為了方便查詢時作為示例。
話不多說見代碼注釋:
DTD文件:SwordTypeDefinition.dtd
XML文件:SwordLib.xml
SwordLibrary?SYSTEM?"SwordTypeDefinition.dtd">
歡欣之刃
1000
10
夜叉
2050
30
魔棒
200
0
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
{
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
{
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
{
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*/
}
}
總結
以上是生活随笔為你收集整理的java 递归遍历对象所有属性_Java学习之Xml系列二:xml按条件查询、xml递归遍历所有元素和属性...的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: mysql with as 用法_Pyt
- 下一篇: java io编程_Java_IO编程