[英]DOM Parser query in JAVA
<subjectOf typeCode="SUBJ">
<annotation classCode="ACT" moodCode="EVN">
<realmCode code="QD" />
<code code="SPECIALNOTE"></code>
<text><![CDATA[<strong>** New York State approval pending. This test is not available for New York State patient testing **</br> ]]></text>
</annotation>
</subjectOf>
<subjectOf typeCode="SUBJ">
<annotation classCode="ACT" moodCode="EVN">
<realmCode code="QD" />
<code code="PREFERREDSPECIMEN"></code>
<text><![CDATA[2 mL Second void urine <strong>or </strong>2-hour urine <strong>or </strong> 2 mL Urine with no preservative]]></text>
</annotation>
</subjectOf>
在DOM解析中,如何遍歷上述XML並根據具有給定值的<code>
標記屬性獲取<text>
標記值。 例如,我想獲得以下文本:
<strong>
**紐約州正在審批中。 此測試不適用於紐約州患者測試**</br>
...基於具有code
屬性(其中value="SPECIALNOTE"
的<code>
標記)。
public static void main(String[] args) throws ParserConfigurationException, SAXException, IOException, XPathExpressionException {
DocumentBuilderFactory domFactory = DocumentBuilderFactory.newInstance();
domFactory.setNamespaceAware(true);
DocumentBuilder builder = domFactory.newDocumentBuilder();
Document doc = builder.parse("xml.xml");
XPath xpath = XPathFactory.newInstance().newXPath(); // XPath Query for showing all nodes value
XPathExpression expr = xpath.compile("/testCodeIdentifier/subjectOf/subjectOf/annotation/code[@code='SPECIALNOTE']");
Object result = expr.evaluate(doc, XPathConstants.NODESET);
NodeList nodes = (NodeList) result;
for (int i = 0; i < nodes.getLength(); i++) {
System.out.println("........"+nodes.item(i).getNodeValue()+"........");
}
}
}
提前感謝幫助...
像這樣修復您的XPath表達式:
/testCodeIdentifier/subjectOf/annotation[code/@code='SPECIALNOTE']/text
然后,您可以使用以下方法訪問CDATA內容:
Node.getTextContent();
更新 :上面的XPath在我發布時似乎是正確的。 同時,您已經完全更改了XML代碼,現在,XPath將讀取
/testCodeIdentifier/subjectOf/code/subjectOf/annotation[code/@code='SPECIALNOTE']/text
或者,因為我猜這個問題太亂了,所以仍然是錯誤的,請執行以下操作:
//annotation[code/@code='SPECIALNOTE']/text
首先,您的XPath表達式有錯誤; subjectOf
不必要地重復:
/subjectOf/subjectOf
現在,假設您確實確實需要引用目標text
元素之前的code
節點,然后使用以下代碼:
XPathExpression expr = xpath.compile(
"/testCodeIdentifier/subjectOf/annotation/code[@code='SPECIALNOTE']");
Node node = (Node) expr.evaluate(doc, XPathConstants.NODE);
System.out.println(getNextElementSibling(node).getTextContent());
其中getNextElementSibling
的定義如下:
public static Node getNextElementSibling(Node node) {
Node next = node;
do {
next = next.getNextSibling();
} while ((next != null) && (next.getNodeType() != Node.ELEMENT_NODE));
return next;
}
關於此的一些注意事項:
getNextSibling
最初對您不起作用的原因是(最可能的),因為所引用code
元素的下一個同級是文本節點,而不是元素節點。 ( code
和text
之間的空白很重要。)這就是為什么我們需要getNextElementSibling
的原因。 XPathConstants.NODE
則使用XPathConstants.NODELIST
請注意,您可能應該按照@Lukas的建議進行操作,並修改XPath表達式以直接選擇目標文本。
以下是直接獲取文本(作為字符串)的方法:
XPathExpression expr = xpath.compile(
"/testCodeIdentifier/subjectOf/annotation[code/@code='SPECIALNOTE']/text/text()");
String text = (String) expr.evaluate(doc, XPathConstants.STRING);
System.out.println(text);
以下是如何首先獲取元素的引用,然后檢索其CDATA部分的內容的方法:
XPathExpression expr = xpath.compile(
"/testCodeIdentifier/subjectOf/annotation[code/@code='SPECIALNOTE']/text");
Node text = (Node) expr.evaluate(doc, XPathConstants.NODE);
System.out.println(text.getTextContent());
最后,我一個人得到了我的問題的答案。...下面的代碼正在解析我的XML。
XPath xpath = XPathFactory.newInstance().newXPath();
// XPath Query for showing all nodes value
XPathExpression expr = xpath.compile("//testCodeIdentifier/subjectOf/order/subjectOf/annotation/code[@code='SPECIALNOTE']/following-sibling::text/text()");
Object result = expr.evaluate(doc, XPathConstants.NODESET);
NodeList nodes = (NodeList) result;
for (int i = 0; i < nodes.getLength(); i++) {
System.out.println(nodes.item(i).getNodeValue());
}
謝謝那些在這篇文章中煩惱的人,但這是一個可行的解決方案。 在上面標記。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.