简体   繁体   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>

In DOM parsing, how can I traverse through the above XML and get the <text> tag value depending upon a <code> tag attribute having a given value. 在DOM解析中,如何遍历上述XML并根据具有给定值的<code>标记属性获取<text>标记值。 For example, I want to get the following text: 例如,我想获得以下文本:

<strong> ** New York State approval pending. <strong> **纽约州正在审批中。 This test is not available for New York State patient testing ** </br> 此测试不适用于纽约州患者测试** </br>

...based on the <code> tag with a code attribute where value="SPECIALNOTE" . ...基于具有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()+"........");      
        }   
    } 
}

Appreciate the help in advance... 提前感谢帮助...

Fix your XPath expression like this: 像这样修复您的XPath表达式:

/testCodeIdentifier/subjectOf/annotation[code/@code='SPECIALNOTE']/text

You could then, for instance, access the CDATA content using 然后,您可以使用以下方法访问CDATA内容:

Node.getTextContent();

UPDATE : The above XPath seemed correct at the time I posted it. 更新 :上面的XPath在我发布时似乎是正确的。 In the meantime, you have completely changed your XML code and now, the XPath would read 同时,您已经完全更改了XML代码,现在,XPath将读取

/testCodeIdentifier/subjectOf/code/subjectOf/annotation[code/@code='SPECIALNOTE']/text

Or, because I am guessing that this question is so messy, it's still wrong, just do: 或者,因为我猜这个问题太乱了,所以仍然是错误的,请执行以下操作:

//annotation[code/@code='SPECIALNOTE']/text

First, your XPath expression has an error; 首先,您的XPath表达式有错误; subjectOf is repeated unnecessarily: subjectOf不必要地重复:

/subjectOf/subjectOf

Now, assuming you really do need a reference to the code node that precedes the target text element, then use the following: 现在,假设您确实确实需要引用目标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());

Where getNextElementSibling is defined as follows: 其中getNextElementSibling的定义如下:

public static Node getNextElementSibling(Node node) {
    Node next = node;
    do {
        next = next.getNextSibling();
    } while ((next != null) && (next.getNodeType() != Node.ELEMENT_NODE));
    return next;
}

A couple of notes about this: 关于此的一些注意事项:

  • The reason that getNextSibling did not originally work for you is (most likely) because the next sibling of the referenced code element is a text node, not an element node. getNextSibling最初对您不起作用的原因是(最可能的),因为所引用code元素的下一个同级是文本节点,而不是元素节点。 (The whitespace between code and text is significant.) That's why we need getNextElementSibling . codetext之间的空白很重要。)这就是为什么我们需要getNextElementSibling的原因。
  • We're selecting a single node, so we're using XPathConstants.NODE instead if XPathConstants.NODELIST 我们选择一个节点,因此如果使用XPathConstants.NODE则使用XPathConstants.NODELIST

Note that you should probably just do as @Lukas suggests and modify your XPath expression to directly select the target text. 请注意,您可能应该按照@Lukas的建议进行操作,并修改XPath表达式以直接选择目标文本。

Here's how to get the text directly (as a String): 以下是直接获取文本(作为字符串)的方法:

XPathExpression expr = xpath.compile(
    "/testCodeIdentifier/subjectOf/annotation[code/@code='SPECIALNOTE']/text/text()");
String text = (String) expr.evaluate(doc, XPathConstants.STRING);
System.out.println(text);

Here's how to first get a reference to the element and then retrieve the contents of its CDATA section: 以下是如何首先获取元素的引用,然后检索其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());

Finally i have got the answer for my question by myself.... Below code is being working for my XML to be parsed... 最后,我一个人得到了我的问题的答案。...下面的代码正在解析我的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()); 

  }

Thank you people who have ansewered in this post but this is a possible solution for it. 谢谢那些在这篇文章中烦恼的人,但这是一个可行的解决方案。 Have a mark on it. 在上面标记。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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