<book>
<item>
<name_zh></name_zh>
</item>
<item>
<name_en></name_en>
</item>
<item>
<name_jp></name_jp>
</item>
</book>
如上xml。。
我想要得到book下各个item 下面的子节点中的值,,一时间想不到啥好方法。各位高手帮忙看看。。。。
------解决方案--------------------
String xml="<book><item><name_zh>gfd</name_zh></item><item><name_en>hgf</name_en></item><item><name_jp>shfj</name_jp></item></book>";
Document doc=DocumentHelper.parseText(xml);
List<Element> list=doc.selectNodes("/book//item/*");
for(Element item:list){
System.out.println(item.getText());
}
------解决方案--------------------
package com.easymap.common;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
public class StaticReadXml {
public static DocumentBuilderFactory dbf=DocumentBuilderFactory.newInstance();
public static DocumentBuilder db=null;
public static Document d=null;
public static String fileNameString = "";
//根据节点名称读取值
public static String ReadValue(String node)
{
String value="";
try {
db=dbf.newDocumentBuilder();
fileNameString = ReadXml.class.getResource("/config.xml").getPath();
d=db.parse(fileNameString);
Element root=d.getDocumentElement();
//获得指定的节点对象
NodeList dl = root.getElementsByTagName(node);
if(dl.getLength()==1){
Element e = (Element)dl.item(0);
value = e.getFirstChild().getNodeValue();
}
} catch (Exception e) {
}
return value;
}
}
------解决方案--------------------
String xml="<book><item><name_zh>gfd</name_zh><price>1649</price></item><item><name_en>hgf</name_en><price>9879</price></item><item><name_jp>shfj</name_jp><price>9412</price></item></book>";
Document doc=DocumentHelper.parseText(xml);
List<Element> list=doc.selectNodes("/book//item/*[starts-with(name(),'name')]");
for(Element item:list){
System.out.println(item.getText());
}