<xml> <responseXml>
<CreateTime>12345678</CreateTime>
<MsgType>new</MsgType>
<ArticleCount>1</ArticleCount>
<Articles>
<item>
<Title>aa</Title>
<Description>aaa
</Description>
<PicUrl>aa
</PicUrl>
<Url>aa</Url>
</item>
</Articles>
</responseXml>
</xml>我想取item 中 Title 和 Description 的值 。
<CreateTime>12345678</CreateTime>
<MsgType>new</MsgType>
<ArticleCount>1</ArticleCount>
<Articles>
<item>
<Title>aa</Title>
<Description>aaa
</Description>
<PicUrl>aa
</PicUrl>
<Url>aa</Url>
</item>
</Articles>
</responseXml>
</xml>我想取item 中 Title 和 Description 的值 。
DocumentBuilder db = dbf.newDocumentBuilder(); Document doc = db.parse(new File(xmlFile));
Element element = doc.getDocumentElement();
System.out.println("root:" + element.getTagName()); NodeList nodeList = doc.getElementsByTagName("item");
System.out.println("nodeList lenth:" + nodeList.getLength()); Node fatherNode = nodeList.item(0);
System.out.println("fatherNode:" + fatherNode.getNodeName()); NamedNodeMap attributes = fatherNode.getAttributes();
for (int i = 0; i < attributes.getLength(); i++) {
Node attribute = attributes.item(i);
System.out.println("NodeName:" + attribute.getNodeName() + " NodeValue:" + attribute.getNodeValue());
} NodeList childNodes = fatherNode.getChildNodes();
System.out.println(childNodes.getLength());
for (int j = 0; j < childNodes.getLength(); j++) {
Node childNode = childNodes.item(j); if (childNode instanceof Element) {
System.out.println("NodeName:" + childNode.getNodeName() + " NodeValue:" + childNode.getFirstChild().getNodeValue()); }
} }
Match m = $(input);
String title = m.find("Title").text();
String description = m.find("Description").text();
aa
description
import java.io.File;
import java.util.ArrayList;
import java.util.List;import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;public class XMLReader {
public static void main(String[] args) throws DocumentException {
File file = new File("d:/xml.xml");
SAXReader reader = new SAXReader();
Document doc = reader.read(file);
Element element = doc.getRootElement();
ResponseXML root = new ResponseXML();
fillRootElement(root, element); if (root != null) {
List<Item> items = root.getArticles().getItems();
for (Item item : items) {
System.out.println("Title:" + item.getTitle()
+ "\tDescription:" + item.getDescription());
}
}
} private static void fillRootElement(ResponseXML root, Element element) {
if (!element.getName().equals("xml")) {
return;
}
Element response = element.element("responseXml");
if (response == null) {
return;
}
String str = response.elementText("CreateTime");
if (str == null || str.isEmpty()) {
return;
}
root.setTime(str); str = response.elementText("MsgType");
if (str == null || str.isEmpty()) {
return;
}
root.setType(str); str = response.elementText("ArticleCount");
if (str == null || str.isEmpty()) {
return;
}
root.setCount(Integer.parseInt(str)); root.setArticles(fillArticles(response)); } private static Articles fillArticles(Element ele) {
Articles articles = new Articles();
Element ele_articles = ele.element("Articles");
List<?> ele_items = ele_articles.elements("item");
Element ele_Item = null;
Item item = null;
for (Object object : ele_items) {
ele_Item = (Element) object;
item = loadItemElement(ele_Item);
if (item == null) {
continue;
}
articles.getItems().add(item);
} return articles;
} private static Item loadItemElement(Element ele_Item) {
Item item = new Item();
String title = ele_Item.elementText("Title");
if (title == null || title.isEmpty()) {
return null;
}
item.setTitle(title); String des = ele_Item.elementText("Description");
if (des == null || des.isEmpty()) {
return null;
}
item.setDescription(des);
return item;
}}class ResponseXML {
String time;
String type;
int count;
Articles articles = new Articles(); public String getTime() {
return time;
} public void setTime(String time) {
this.time = time;
} public String getType() {
return type;
} public void setType(String type) {
this.type = type;
} public int getCount() {
return count;
} public void setCount(int count) {
this.count = count;
} public Articles getArticles() {
return articles;
} public void setArticles(Articles articles) {
this.articles = articles;
}}class Articles {
private List<Item> items = new ArrayList<Item>(); public List<Item> getItems() {
return items;
}}class Item {
String title;
String description;
String picUrl;
String url; public String getTitle() {
return title;
} public void setTitle(String title) {
this.title = title;
} public String getDescription() {
return description;
} public void setDescription(String description) {
this.description = description;
} public String getPicUrl() {
return picUrl;
} public void setPicUrl(String picUrl) {
this.picUrl = picUrl;
} public String getUrl() {
return url;
} public void setUrl(String url) {
this.url = url;
}}这段代码因为写的很急,所以中间有些异常可能没有处理到位,但是实现你所说的功能应该是没有问题的。
<xml>
<responsexml createtime="12345678" msgtype="new" articlecount="1">
<articles>
<item title="aa" description="aaa" picurl="aa" url="url"/>
<item title="bb" description="bbb" picurl="bb" url="url"/>
</articles>
</responsexml>
</xml>