如何使用Jdom

时间:2018-01-03 06:56:30

标签: java xpath xml-parsing jdom

我正在尝试使用Jdom解析器从XML获取特定值。 下面是我的xml:

<recordTarget>
    <patientRole>
      **<id root="1.20.3.01.5.2" extension="a"/>
      <id root="1.2.0.5.1.3.2" extension="b"/>**
      <addr use=""><country></country><state></state><city></city><postalCode></postalCode><streetAddressLine></streetAddressLine></addr>
      <telecom value="" use=""/>
      <telecom value="" use=""/>
      <patient>
      </patient>
      <providerOrganization>
      </providerOrganization>
    </patientRole>
  </recordTarget>

现在从上面的xml我希望获得&#39;扩展&#39; &#39; ID&#39;下的属性标记(标记为asterik),其值为&#34; 3.2&#34;在其中忽略包含&#34; 5.2&#34;。

的id标签

我能够获得第一个值,但我需要获得第二个id标记值。

下面是我的java代码,它给出了ID扩展名的第一个值:

XPathExpression<Attribute> expr = xFactory.compile(xPath, Filters.attribute(), null, defaultNs);
        Attribute attribute = expr.evaluateFirst(document);
        if (attribute != null) {
            return attribute.getValue();
        } else {
            return "";
        }

2 个答案:

答案 0 :(得分:0)

您没有显示您正在使用的实际xPath是什么,但我会想象xPath类似于:

//id[contains(@root, '3.2')]/@extension

应该这样做。

对我来说,我用它来运行:

    String xPath = "//id[contains(@root, '3.2')]/@extension";

    XPathFactory xFactory = XPathFactory.instance();
    XPathExpression<Attribute> expr = xFactory.compile(xPath, Filters.attribute());
    Attribute attribute = expr.evaluateFirst(document);
    if (attribute != null) {
        System.out.println(attribute.getValue());
    } else {
        System.out.println("foobar");
    }

请注意,我使用了contains(..., ...),但规范还有其他搜索文字的选项,请参阅documentation

答案 1 :(得分:-1)

您可以获得一个具体ID

的扩展名
import java.io.IOException;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathExpression;
import javax.xml.xpath.XPathExpressionException;
import javax.xml.xpath.XPathFactory;

import org.junit.Test;
import org.w3c.dom.Document;
import org.xml.sax.SAXException;

public class XmlDomTest {

@Test
public void getSecondIdFromXml() {

    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    factory.setNamespaceAware(true);
    DocumentBuilder builder;
    Document doc = null;
    try {
        builder = factory.newDocumentBuilder();
        doc = builder.parse(getClass().getClassLoader().getResourceAsStream("your_file"));

        XPathFactory xpathFactory = XPathFactory.newInstance();

        XPath xpath = xpathFactory.newXPath();

        System.out.println("Extension: " + getExtensionById(doc, xpath, "1.2.0.5.1.3.2"));

    } catch (ParserConfigurationException | SAXException | IOException e) {
        e.printStackTrace();
    }

}

private String getExtensionById(Document doc, XPath xpath, String id) {
    String value= null;
    try {
        XPathExpression expr =  xpath.compile("//id[@root='" + id + "']/@extension");
        value= (String) expr.evaluate(doc, XPathConstants.STRING);
    } catch (XPathExpressionException e) {
        e.printStackTrace();
    }

    return value;
}}