有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java使用XPath从xml文档中提取值的方法是什么?

我目前正在从表单中提取数据。我能够提取rpm类型和属性,如ccoid\U键、ccoid\U版本,但我无法提取“190”。 我使用以下语法“//form///rpm type/”并在http://www.xpathtester.com/test上测试了它,它似乎返回了我想要的

<form xmlns:ev="http://www.w3.org/2001/xml-events"     xmlns:exforms="http://www.exforms.org/exf/1-0" xmlns:fr="http://orbeon.org/oxf/xml/form-    runner" xmlns:ns2="http://db.rpm" xmlns:ns3="http://wscommon.rpm"    xmlns:ns4="http://form.rpm" xmlns:ns5="http://form.rpm" xmlns:pipeline="java:org.orbeon.oxf.processor.pipeline.PipelineFunctionLibrary" xmlns:saxon="http://saxon.sf.net/" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:sql="http://orbeon.org/oxf/xml/sql" xmlns:xbl="http://www.w3.org/ns/xbl" xmlns:xforms="http://www.w3.org/2002/xforms" xmlns:xhtml="http://www.w3.org/1999/xhtml" xmlns:xi="http://www.w3.org/2001/XInclude" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xxforms="http://orbeon.org/oxf/xml/xforms" xmlns:xxi="http://orbeon.org/oxf/xml/xinclude">
<section-data>
<date>2012-06-12Z</date>
<author-name>admin admin</author-name>
<note>note</note>
</section-data>
<section-2>
<control-4>
<rpm-type ccoid_key="K000001" ccoid_version="1" type="rpm-weight">
   <rpm-value ccoid_key="K000002" type="rpm-weight-kg">190</rpm-value>
</rpm-type> 
</control-4>
<control-5>
<rpm-type ccoid_key="K000001" ccoid_version="1" type="rpm-weight">
    <rpm-value ccoid_key="K000002" type="rpm-weight-lbs">222</rpm-value>
    <rpm-value ccoid_key="K000003" type="rpm-weight-oz">21</rpm-value>
</rpm-type>
</control-5>
</section-2>
</form>

我正在使用node。getNodeValue()但我得到一个空值。。。 从日志中,我可以看出我能够提取许多属性及其值, 但是当尝试获取childNode的值时,我得到null:

2012-06-12 14:17:49028[http-thread-pool-1234(2)]调试[FormSrvcImpl:944]路径:[//form///rpm type/] 2012-06-12 14:17:49039[http-thread-pool-1234(2)]调试[FormSrvcImpl:1007]找到的节点值:null 2012-06-12 14:17:49040[http-thread-pool-1234(2)]调试[FormSrvcImpl:1054]要添加的临床单位:ClinicalUnit[idCcUnit=,idCc=0,CCOIDUNTKey=K000002,CCOIDUNTVersion=0] 2012-06-12 14:17:49041[http-thread-pool-1234(2)]调试[FormsRvCompl:1055]要添加的临床类型:ClinicalType[idCcClinicalType=,idCc=0,CCOIDCClinicalTypeKey=rpm权重,CCOIDCClinicalTypeVersion=1,valueType=null,units=null] 要添加的临床值:ClinicalValue[id_Clinical_Value=0,clinicalType=clinicalType[idCcClinicalType=,idCc=0,CCOIDCClinicalTypeKey=rpm权重,CCOIDCClinicalTypeVersion=1,valueType=null,units=null],ClinicalValue=null,valueDate=null,status=ACTIVE,sourceType=ORBEON_FORM,idSource=18585,idPatient=219,clinicalUnit=null]


共 (3) 个答案

  1. # 1 楼答案

    根据Javadoc ^{},如果调用某种节点,则返回null(参见上一链接开头的表)。您可能应该改用^{}

  2. # 2 楼答案

    在我看来,似乎您试图在rpm类型(没有值,只有子节点)上使用getNodeValue(),而不是在rpm值节点上使用您正在查找的190值

  3. # 3 楼答案

    出于某种原因,我在节点中找到了该值。getTextContent