Document XML 取值问题
<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 的值 。 --------------------编程问答-------------------- 你打算用什么取呀?JS还是DOM4j什么的? --------------------编程问答-------------------- 使用xpath,或者joox 非常简单 --------------------编程问答-------------------- 如果用w3c的DOM怎么取? --------------------编程问答--------------------
http://www.mkyong.com/java/how-to-read-xml-file-in-java-dom-parser/ --------------------编程问答--------------------
如果用w3c的DOM怎么取?
使用xpath,或者joox 非常简单
http://www.mkyong.com/java/how-to-read-xml-file-in-java-dom-parser/
MlConfig(String xmlFile) throws Exception {--------------------编程问答--------------------
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
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());
}
}
}
import static org.joox.JOOX.*;--------------------编程问答-------------------- xml/responseXml/Articles/
Match m = $(input);
String title = m.find("Title").text();
String description = m.find("Description").text();
aa
description --------------------编程问答-------------------- 你可以使用SaxReader,不多说,直接上代码:
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的定义结构也不是很好:我改了一下你的xml的结构定义,参见如下:
<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>
补充:Java , Java SE