Java使用DOM解析XML响应

时间:2014-03-26 17:18:16

标签: java xml parsing dom

我想抓住以下网址的回复:

http://eutils.ncbi.nlm.nih.gov/entrez/eutils/esearch.fcgi?db=pubmed&term=ACE+OR+%28Adverse+AND+Childhood+AND+Experiences%29

我有以下代码:

DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
org.w3c.dom.Document doc = db.parse(new URL("http://eutils.ncbi.nlm.nih.gov/entrez/eutils/esearch.fcgi?db=pubmed&term=ACE+OR+%28Adverse+AND+Childhood+AND+Experiences%29").openStream());

当我这样做时:

System.out.println ("Root element: " + doc.getDocumentElement().getNodeName());

我明白了:

Root element: eSearchResult

这是正确的,但我不知道在第一个" Count"节点,这不起作用:

String count = doc.getFirstChild().getChildNodes().item(0).getTextContent();

1 个答案:

答案 0 :(得分:1)

试试这个:

System.out.println ("First Count Text: " + doc.getElementsByTagName("Count").item(0).getTextContent());

,或者

doc.getDocumentElement().getFirstChild().getTextContent();