【发布时间】:2015-07-18 15:53:07
【问题描述】:
我有以下 XML 文件:
<RecordSet>
<Record>
<ID>001</ID>
<TermList>
<Term>Term1</Term>
<Term>Term2</Term>
<Term>Term3</Term>
</TermList>
</Record>
<Record>
<ID>002</ID>
<TermList>
<Term>Term3</Term>
<Term>Term4</Term>
<Term>Term5</Term>
</TermList>
</Record>
</RecordSet>
并需要将其解析为“ID-Term”文件,即,
001 Term1
001 Term2
001 Term3
002 Term3
002 Term4
002 Term5
目前我有以下应用:
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import javax.xml.parsers.*;
import javax.xml.xpath.*;
import org.w3c.dom.Document;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;
public class MedlineParser {
public static void main(String[] args) {
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
factory.setNamespaceAware(true);
DocumentBuilder builder;
Document doc = null;
try {
builder = factory.newDocumentBuilder();
doc = builder.parse("/home/andrej/Documents/test.xml");
// Create XPathFactory object
XPathFactory xpathFactory = XPathFactory.newInstance();
// Create XPath object
XPath xpath = xpathFactory.newXPath();
try {
XPathExpression expr1 = xpath.compile("/RecordSet/Record/ID/text()");
NodeList nodes1 = (NodeList) expr1.evaluate(doc, XPathConstants.NODESET);
for (int i = 0; i < nodes1.getLength(); i++) {
String id = nodes1.item(i).getNodeValue();
XPathExpression expr2 = xpath.compile("/RecordSet/Record/TermList/Term/text()");
NodeList nodes2 = (NodeList) expr2.evaluate(doc, XPathConstants.NODESET);
for (int j = 0; j < nodes2.getLength(); j++) {
System.out.println(id + " " + nodes2.item(i).getNodeValue());
}
}
} catch (XPathExpressionException e) {
e.printStackTrace();
}
} catch (IOException | ParserConfigurationException | SAXException e) {
e.printStackTrace();
}
}
}
很遗憾,目前程序输出是:
001 Term1
001 Term1
001 Term1
001 Term1
001 Term1
001 Term1
002 Term2
002 Term2
002 Term2
002 Term2
002 Term2
002 Term2
知道 XPath 表达式有什么问题吗?
【问题讨论】: