草庐IT

java - xpath 在一次调用中解析多个值

coder 2024-06-29 原文

如何在一次调用中获取多个路径的 xPath 值。

例如

<Message version="010" release="006"  xmlns="http://www.ncpdp.org/schema/SCRIPT" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<Header>
</Header>
<Body>
    <CommunicationNumbers>
        <Communication>
            <Number>5551234444</Number>
            <Qualifier>TE</Qualifier>
        </Communication>
        <Communication>
            <Number>5551235555</Number>
            <Qualifier>FX</Qualifier>
        </Communication>
    </CommunicationNumbers>
    <Identification>
        <FileID>616</FileID>
        <DEANumber>AB123456</DEANumber>
        <SocialSecurity>123456789</SocialSecurity>
    </Identification>
    <Specialty>A</Specialty>
    <ClinicName>Therapy Department</ClinicName>
    <Name>
        <LastName>Xavior</LastName>
        <FirstName>Charles</FirstName>
        <MiddleName>C</MiddleName>
        <Suffix>MD</Suffix>
    </Name>
    <Address>
        <AddressLine1>888 ABC Drive</AddressLine1>
        <AddressLine2>Suite 200</AddressLine2>
        <City>Miami</City>
        <State>FL</State>
        <ZipCode>12345</ZipCode>
    </Address>
</Body>

我需要以下值:

:通讯/号码 :标识/文件ID :专业

在一次通话中。

我使用的是单个值

 public static String getExpValue(final String xmlString, final String expression, final ServiceNamespaceContext nameSpace) throws XPathExpressionException {
    StringReader strReader = new StringReader(xmlString);
    InputSource inputStr = new InputSource(strReader);
    String result = null;
    try {
        final XPath xpath = XPathFactory.newInstance().newXPath();
        xpath.setNamespaceContext(nameSpace);

        final XPathExpression expr = xpath.compile(expression);
        result = (String) expr.evaluate(inputStr, XPathConstants.STRING);
    } finally {
        strReader = null;
        inputStr = null;
    }
    return result;
}

我想要的输出是一个连接的字符串 5551234444616A

最佳答案

你可以尝试使用类似...

XPathExpression expr = xpath.compile("//Communication/Number | //Identification/FileID");

应该结合每个查询的结果。在我的(简单)测试中,我得到了 3 个匹配项(2 个用于 Communication/Number,1 个用于 Identification/FileID)

已更新

预期的结果是返回一个 NodeList,例如...

NodeList nl = (NodeList)expr.evaluate(inputStr, XPathConstants.NODELIST);
for (int index = 0; index < nl.getLength(); index++) {
    Node node = nl.item(index);
    String value = node.getTextContent();
    System.out.println(value);
}

关于java - xpath 在一次调用中解析多个值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24544229/

有关java - xpath 在一次调用中解析多个值的更多相关文章

  1. ruby - 如何使用 Nokogiri 的 xpath 和 at_xpath 方法 - 2

    我正在学习如何使用Nokogiri,根据这段代码我遇到了一些问题:require'rubygems'require'mechanize'post_agent=WWW::Mechanize.newpost_page=post_agent.get('http://www.vbulletin.org/forum/showthread.php?t=230708')puts"\nabsolutepathwithtbodygivesnil"putspost_page.parser.xpath('/html/body/div/div/div/div/div/table/tbody/tr/td/div

  2. Ruby 解析字符串 - 2

    我有一个字符串input="maybe(thisis|thatwas)some((nice|ugly)(day|night)|(strange(weather|time)))"Ruby中解析该字符串的最佳方法是什么?我的意思是脚本应该能够像这样构建句子:maybethisissomeuglynightmaybethatwassomenicenightmaybethiswassomestrangetime等等,你明白了......我应该一个字符一个字符地读取字符串并构建一个带有堆栈的状态机来存储括号值以供以后计算,还是有更好的方法?也许为此目的准备了一个开箱即用的库?

  3. ruby-on-rails - Rails 3 中的多个路由文件 - 2

    Rails2.3可以选择随时使用RouteSet#add_configuration_file添加更多路由。是否可以在Rails3项目中做同样的事情? 最佳答案 在config/application.rb中:config.paths.config.routes在Rails3.2(也可能是Rails3.1)中,使用:config.paths["config/routes"] 关于ruby-on-rails-Rails3中的多个路由文件,我们在StackOverflow上找到一个类似的问题

  4. ruby-on-rails - 在 Ruby 中循环遍历多个数组 - 2

    我有多个ActiveRecord子类Item的实例数组,我需要根据最早的事件循环打印。在这种情况下,我需要打印付款和维护日期,如下所示:ItemAmaintenancerequiredin5daysItemBpaymentrequiredin6daysItemApaymentrequiredin7daysItemBmaintenancerequiredin8days我目前有两个查询,用于查找maintenance和payment项目(非排他性查询),并输出如下内容:paymentrequiredin...maintenancerequiredin...有什么方法可以改善上述(丑陋的)代

  5. ruby - 解析 RDFa、微数据等的最佳方式是什么,使用统一的模式/词汇(例如 schema.org)存储和显示信息 - 2

    我主要使用Ruby来执行此操作,但到目前为止我的攻击计划如下:使用gemsrdf、rdf-rdfa和rdf-microdata或mida来解析给定任何URI的数据。我认为最好映射到像schema.org这样的统一模式,例如使用这个yaml文件,它试图描述数据词汇表和opengraph到schema.org之间的转换:#SchemaXtoschema.orgconversion#data-vocabularyDV:name:namestreet-address:streetAddressregion:addressRegionlocality:addressLocalityphoto:i

  6. ruby-on-rails - Rails - 一个 View 中的多个模型 - 2

    我需要从一个View访问多个模型。以前,我的links_controller仅用于提供以不同方式排序的链接资源。现在我想包括一个部分(我假设)显示按分数排序的顶级用户(@users=User.all.sort_by(&:score))我知道我可以将此代码插入每个链接操作并从View访问它,但这似乎不是“ruby方式”,我将需要在不久的将来访问更多模型。这可能会变得很脏,是否有针对这种情况的任何技术?注意事项:我认为我的应用程序正朝着单一格式和动态页面内容的方向发展,本质上是一个典型的网络应用程序。我知道before_filter但考虑到我希望应用程序进入的方向,这似乎很麻烦。最终从任何

  7. ruby - 用逗号、双引号和编码解析 csv - 2

    我正在使用ruby​​1.9解析以下带有MacRoman字符的csv文件#encoding:ISO-8859-1#csv_parse.csvName,main-dialogue"Marceu","Giveittohimóhe,hiswife."我做了以下解析。require'csv'input_string=File.read("../csv_parse.rb").force_encoding("ISO-8859-1").encode("UTF-8")#=>"Name,main-dialogue\r\n\"Marceu\",\"Giveittohim\x97he,hiswife.\"\

  8. ruby - 多个属性的 update_column 方法 - 2

    我有一个具有一些属性的模型:attr1、attr2和attr3。我需要在不执行回调和验证的情况下更新此属性。我找到了update_column方法,但我想同时更新三个属性。我需要这样的东西:update_columns({attr1:val1,attr2:val2,attr3:val3})代替update_column(attr1,val1)update_column(attr2,val2)update_column(attr3,val3) 最佳答案 您可以使用update_columns(attr1:val1,attr2:val2

  9. java - 等价于 Java 中的 Ruby Hash - 2

    我真的很习惯使用Ruby编写以下代码:my_hash={}my_hash['test']=1Java中对应的数据结构是什么? 最佳答案 HashMapmap=newHashMap();map.put("test",1);我假设? 关于java-等价于Java中的RubyHash,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.com/questions/22737685/

  10. ruby-on-rails - 在 ruby​​ .gemspec 文件中,如何指定依赖项的多个版本? - 2

    我正在尝试修改当前依赖于定义为activeresource的gem:s.add_dependency"activeresource","~>3.0"为了让gem与Rails4一起工作,我需要扩展依赖关系以与activeresource的版本3或4一起工作。我不想简单地添加以下内容,因为它可能会在以后引起问题:s.add_dependency"activeresource",">=3.0"有没有办法指定可接受版本的列表?~>3.0还是~>4.0? 最佳答案 根据thedocumentation,如果你想要3到4之间的所有版本,你可以这

随机推荐