【问题标题】:How to get a particular tag value using Jdom如何使用 Jdom 获取特定的标签值
【发布时间】:2018-06-12 20:18:54
【问题描述】:

我正在尝试使用 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 中,我想在(以星号标记)中的“ID”标签下获取“扩展”属性,其中包含值“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


    【解决方案1】:

    您没有显示您正在使用的实际 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

    【讨论】:

    • 我使用 xpath 作为 //recordTarget/patientRole/id/@extension 并且我正在从属性文件中获取 xpath
    【解决方案2】:

    您可以获得一个具体 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;
    }}
    

    【讨论】:

    • @Sergio - 实际上 ID 不是具体的,但它将包含“3.2”,我需要验证并选择该根的扩展名。上面的 rolfl 答案通过更改 xpath 起作用。
    猜你喜欢
    • 1970-01-01
    • 2014-11-04
    • 1970-01-01
    • 2018-01-25
    • 1970-01-01
    • 1970-01-01
    • 2012-12-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多