java中jdom讀取xml的問題 本帖最后由 peng13425 于 2012-12-28 17:11:38 编辑 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 使用 XPath //*[text()='1'] 可以得到<c02_physical_activity>1</c02_physical_activity><c03_healthy_weight>1</c03_healthy_weight> import java.io.*;import javax.xml.xpath.*;import org.w3c.dom.*;import org.xml.sax.*;public class XPathExample { public static void main(final java.lang.String[] args) { try (final FileReader reader = new FileReader("xpathexample.xml")){ XPath xpath = XPathFactory.newInstance().newXPath(); String expression = "//*[text()='1']"; InputSource source = new InputSource(reader); NodeList nodes = (NodeList) xpath.evaluate(expression, source, XPathConstants.NODESET); for(int i = 0; i < nodes.getLength(); i++){ System.out.println(nodes.item(i).getNodeName()); } } catch (final IOException | XPathExpressionException e) { System.err.println(e.getMessage()); } }}使用jdk7编译运行。 SAXBuilder bulider =new SAXBuilder(); try { Document doc=bulider.build(JDomDemo.class.getResourceAsStream("NewFile.xml")); Element root=doc.getRootElement(); List<Element> list1=root.getChildren(); for(Element e1:list1) { List<Element> list2=e1.getChildren(); for(Element e2:list2) { if(e2.getTextTrim().equals("1")) { System.out.println(e2.getName()+":"+ e2.getText()); } } } } catch (JDOMException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); }这个没有Xpath,你看看吧 java JButton(按钮)绑定键盘 ★〓一个看似相当简单的问题,可惜我没搞定,请高手指点迷津!!!〓★ Exception in thread "main" java.lang.NoClassDefFoundError: Java的晚绑定用的也是虚函数表那一套吗? 关于Bezier曲线鼠标绘制的思想!!请高手赐教 帮忙看看程序错误。有关HASHTABLE的自定义键类。 如何提出字符串中的字符串? windows下读取linux下的mysql的问题 关于两个不同的类实现同一个接口的问题 为何时针坐标有误? 求助,,找了很多都没人回答,求真正的大神 socket通信 数据阻塞的问题
<c02_physical_activity>1</c02_physical_activity>
<c03_healthy_weight>1</c03_healthy_weight>
import javax.xml.xpath.*;
import org.w3c.dom.*;
import org.xml.sax.*;public class XPathExample {
public static void main(final java.lang.String[] args) {
try (final FileReader reader = new FileReader("xpathexample.xml")){
XPath xpath = XPathFactory.newInstance().newXPath();
String expression = "//*[text()='1']";
InputSource source = new InputSource(reader);
NodeList nodes = (NodeList) xpath.evaluate(expression, source, XPathConstants.NODESET); for(int i = 0; i < nodes.getLength(); i++){
System.out.println(nodes.item(i).getNodeName());
} } catch (final IOException | XPathExpressionException e) {
System.err.println(e.getMessage());
}
}
}使用jdk7编译运行。
try {
Document doc=bulider.build(JDomDemo.class.getResourceAsStream("NewFile.xml"));
Element root=doc.getRootElement();
List<Element> list1=root.getChildren();
for(Element e1:list1)
{
List<Element> list2=e1.getChildren();
for(Element e2:list2)
{
if(e2.getTextTrim().equals("1"))
{
System.out.println(e2.getName()+":"+ e2.getText());
}
}
}
} catch (JDOMException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}这个没有Xpath,你看看吧