繁体   English   中英

JAVA中的DOM解析器查询

[英]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>&nbsp;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元素的下一个同级是文本节点,而不是元素节点。 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());

最后,我一个人得到了我的问题的答案。...下面的代码正在解析我的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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM