如何使用Jdom获取特定标记值

问题描述 投票:1回答:2

我试图使用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中我想获得(标记为asterik)中“ID”标签下的“extension”属性,其中包含值“3.2”,并忽略包含“5.2”的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 "";
        }
java xpath xml-parsing jdom
2个回答
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
投票

您可以获得一个具体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;
}}
© www.soinside.com 2019 - 2024. All rights reserved.