gpt4 book ai didi

java - JAVA 中的 DOM 解析器查询

转载 作者:行者123 更新时间:2023-12-02 00:32:50 25 4
gpt4 key购买 nike

<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>&nbsp;2 mL Urine with no preservative]]></text>
</annotation>
</subjectOf>

在DOM解析中,如何遍历上面的XML并得到<text>标签值取决于 <code>具有给定值的标签属性。例如,我想获取以下文本:

<strong>** New York State approval pending. This test is not available for New York State patient testing **</br>

...基于<code>带有 code 的标签属性其中 value="SPECIALNOTE" .

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 表达式有错误; 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 元素的下一个同级元素是文本节点,而不是元素节点。 (codetext 之间的空格很重要。)这就是我们需要 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());

关于java - JAVA 中的 DOM 解析器查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8560180/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com