如何使用java从xml文件中提取细节?
我有以下类型的XML文件如何使用java从xml文件中提取细节?,java,xml,xml-parsing,Java,Xml,Xml Parsing,我有以下类型的XML文件 <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE eSummaryResult PUBLIC "-//NLM//DTD eSummaryResult, 29 October 2004//EN" "http://www.ncbi.nlm.nih.gov/entrez/query/DTD/eSummary_041029.dtd"> <eSummaryResult> <DocSum>
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE eSummaryResult PUBLIC "-//NLM//DTD eSummaryResult, 29 October 2004//EN" "http://www.ncbi.nlm.nih.gov/entrez/query/DTD/eSummary_041029.dtd">
<eSummaryResult>
<DocSum>
<Id>224589801</Id>
<Item Name="Caption" Type="String">NC_000010</Item>
<Item Name="Title" Type="String">Homo sapiens chromosome 10, GRCh37.p10 Primary Assembly</Item>
<Item Name="Extra" Type="String">gi|224589801|gnl|ASM:GCF_000001305|10|ref|NC_000010.10||gpp|GPC_000000034.1||gnl|NCBI_GENOMES|10[224589801]</Item>
<Item Name="Gi" Type="Integer">224589801</Item>
<Item Name="CreateDate" Type="String">2002/08/29</Item>
<Item Name="UpdateDate" Type="String">2012/10/30</Item>
<Item Name="Flags" Type="Integer">544</Item>
<Item Name="TaxId" Type="Integer">9606</Item>
<Item Name="Length" Type="Integer">135534747</Item>
<Item Name="Status" Type="String">live</Item>
<Item Name="ReplacedBy" Type="String"/>
<Item Name="Comment" Type="String"><![CDATA[ ]]></Item>
</DocSum>
</eSummaryResult>
224589801
NC_000010
智人10号染色体,GRCh37.p10初级装配
gi | 224589801 | gnl | ASM:GCF | 000001305 | 10 |参考| NC | U 000010.10 | gpp | GPC | 0000000 34.1 | gnl | NCBI U基因组| 10[224589801]
224589801
2002/08/29
2012/10/30
544
9606
135534747
居住
如何根据node=“Item”的名称值从中提取详细信息?而且,使用标准的JavaDOMXML还是使用其他xml解析器库更好 试试下面的代码
/* Create a Document object (doc) from the xml */
NodeList list = doc.getElementsByTagName("Item");
for(int i=0;i<list.getLength();i++)
{
Node node = list.item(i);
NamedNodeMap namedNodeMap = node.getAttributes();
if(namedNodeMap.getNamedItem("Name").getTextContent().equalsIgnoreCase("Caption"))
{
System.out.println(node.getTextContent());
}
}
/*从xml创建文档对象(doc)*/
节点列表=doc.getElementsByTagName(“项”);
对于(int i=0;i可能使用XPath
Document dom = ...;
XPath xpath = XPathFactory.newInstance().newXPath();
String result = xpath.evaluate("/eSummaryResult/DocSum/Item[@Name='Title']", dom);
如果只使用标准Java,则XPath是一种方法:
private URL xml = getClass().getResource("/example.xml");
@Test
public void testExamples() throws Exception {
//assertEquals("NC_000010", extractUsingDom("Caption"));
assertEquals("NC_000010", extractUsingXPath("Caption"));
}
public String extractUsingXPath(final String name) throws XPathExpressionException, IOException {
// XPathFactory class is not thread-safe so we do not store it
XPath xpath = XPathFactory.newInstance().newXPath();
return xpath.evaluate(
String.format("/eSummaryResult/DocSum/Item[@Name='%s']", name), // xpath expression
new InputSource(xml.openStream())); // the XML Document
}
我建议使用StAX,试试这个(javax.xml.stream.*)
STAX必须始终是第一件要考虑的事情。看你会知道为什么
XMLInputFactory f = XMLInputFactory.newInstance();
XMLStreamReader rdr = f.createXMLStreamReader(new FileReader("test.xml"));
while (rdr.hasNext()) {
if (rdr.next() == XMLStreamConstants.START_ELEMENT) {
if (rdr.getLocalName().equals("Item")) {
System.out.println(rdr.getAttributeValue("", "Name"));
System.out.println(rdr.getElementText());
}
}
}