67,538
社区成员
发帖
与我相关
我的任务
分享 SAXBuilder sb = new SAXBuilder();
Document doc = sb.build("d:\\d.xml"); XPath xpath = XPath.newInstance("//SourceLocation");
Element SourceLocation = (Element) xpath.selectSingleNode(doc);SourceLocation.getAttributeValue("snippet") XPath xpath2 = XPath.newInstance("//Snippet");
List Snippets = xpath2.selectNodes(doc);
for (int i = 0; i < Snippets.size(); i++) {
Element Snippet = (Element) Snippets.get(i);
if (Snippet.getAttributeValue("id").equals(snippet))
System.out.println(Snippet.getChildText("Text"));
}package cn.ujjboy.answer.csdn;
import java.io.IOException;
import java.util.List;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.JDOMException;
import org.jdom.Namespace;
import org.jdom.input.SAXBuilder;
import org.jdom.xpath.XPath;
public class ReadXML5 {
public static void main(String[] args) throws JDOMException, IOException {
SAXBuilder sb = new SAXBuilder();
Document doc = sb.build("d:\\a.xml");
XPath xpath = XPath.newInstance("//SourceLocation");
Element SourceLocation = (Element) xpath.selectSingleNode(doc);
String snippet = SourceLocation.getAttributeValue("snippet");//取得snippet的值
System.out.println(snippet);
XPath xpath2 = XPath.newInstance("//Snippet");
List Snippets = xpath2.selectNodes(doc);
for (int i = 0; i < Snippets.size(); i++) {
Element Snippet = (Element) Snippets.get(i);
if (Snippet.getAttributeValue("id").equals(snippet))//比较id和snippet的值
System.out.println(Snippet.getChildText("Text"));
}
}
}