日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > java >内容正文

java

java 递归遍历对象所有属性_Java学习之Xml系列二:xml按条件查询、xml递归遍历所有元素和属性...

發布時間:2025/3/12 java 17 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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递归遍历所有元素和属性...的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。