2016-03-04 89 views
1

我使用Xpath提供的'evaluate'函数解析xml字符串以读取嵌入在xml中特定标记中的内容。但是我在试图解析一个嵌入到我想要解析的一组标签中的xml时遇到了麻烦。使用XPath解析另一个xml中嵌入的xml Spring

<Channel> 
<channelId>SD0987</channelId> 
    <parameters> 
<param>org.springframework.messaging.MessageHandlingException: HTTP request execution failed for URI [https://nike-dev.coupahost.com/api/suppliers?return_object=limited]; nested exception is org.springframework.web.client.HttpClientErrorException: 400 Bad Request</param> 
<param><?xml version="1.0" encoding="UTF-8"?> 
<errors> 
<error> 
<![CDATA[Unable to find valid PaymentTerm record for payment_term with keys {"code"=>"9999"}. Possible keys are ["id", "code"]. Please verify your xml.]]> 
</error> 
</errors> 
</param> 
<param>6-5100</param> 
<param><?xml version="1.0" encoding="UTF-8"?><supplier><name>SuTestVDR008-6-5100</name><display-name>SuTestVDR008-6 (SANDALS) - VAT Registration Number - Indirect Europe - INACTIVE</display-name><status>active</status><deleted type="boolean">true</deleted><vendor-account-group-code>1001</vendor-account-group-code><vendor-number>SuTestVDR008-6</vendor-number><on-hold type="boolean">false</on-hold><language-code>E</language-code><purchase-organization-code>5100</purchase-organization-code><group-key>1234</group-key><vendor-partner-type-code>Z4</vendor-partner-type-code><currency-code>EUR</currency-code><primary-address><street1>@@@@ JIU BI VILLAGE</street1><street2>77774</street2><city>GUANGZHOU CITY</city><state>190-Guangdong</state><postal-code>511480</postal-code><country><code>CN</code></country></primary-address><payment-term><code>9999</code></payment-term><content-groups><content-group><name>730_Company Code</name></content-group></content-groups><po-method>prompt</po-method><invoice-matching-level>3-way</invoice-matching-level></supplier></param> 
</parameters> 
</Channel> 

在上面的xml示例中,我需要读取和检索标记“param”中的所有内容。 目前,我首先得到了param标记的计数,然后循环直通他们在标签内获得数据 -

paramCount = XPathUtils.evaluate(originalPayload,"count(/Channel/parameters/param)",XPathUtils.STRING);  
    for(int i=1;i<=pCount;i++){ 
      pList.add(XPathUtils.evaluate(originalPayload, "/Channel/parameters/param[" +i+ "]",XPathUtils.STRING) 
      } 

这工作得很好与其他个XML但与上面给出的样本。我得到下面的错误 - “?xmlversion =”?

"[Fatal Error] :5:17:The processing instruction target matching "[xX][mM][lL]" is not allowed." 

如果我删除了部分1.0" 编码= “UTF-8” 它解析标签,但输出 -

org.springframework.messaging.MessageHandlingException: HTTP request execution failed for URI [https://nike-dev.coupahost.com/api/suppliers?return_object=limited]; nested exception is org.springframework.web.client.HttpClientErrorException: 400 Bad Request 


Unable to find valid PaymentTerm record for payment_term with keys {"code"=>"9999"}. Possible keys are ["id", "code"]. Please verify your xml. 



6-5100 
SuTestVDR008-6-5100SuTestVDR008-6 (SANDALS) - VAT Registration Number - Indirect Europe - [email protected]@@@ JIU BI VILLAGE77774GUANGZHOU CITY190-Guangdong511480CN9999730_Company Codeprompt3-way 

因此,大家可以看到,它只是捕获的各种标签内的内容并打印它。 我问的是打印的所有文本在

之间

<param > 
的标签,与所有的缩进到位。

所以预期输出 -

org.springframework.messaging.MessageHandlingException: HTTP request execution failed for URI [https://nike-dev.coupahost.com/api/suppliers?return_object=limited]; nested exception is org.springframework.web.client.HttpClientErrorException: 400 Bad Request 
<?xml version="1.0" encoding="UTF-8"?> 
<errors> 
<error> 
<![CDATA[Unable to find valid PaymentTerm record for payment_term with keys {"code"=>"9999"}. Possible keys are ["id", "code"]. Please verify your xml.]]> 
</error> 
</errors> 
6-5100 
<?xml version="1.0" encoding="UTF-8"?> 
<supplier><name>SuTestVDR008-6-5100</name><display-name>SuTestVDR008-6 (SANDALS) - VAT Registration Number - Indirect Europe - INACTIVE</display-name><status>active</status><deleted type="boolean">true</deleted><vendor-account-group-code>1001</vendor-account-group-code><vendor-number>SuTestVDR008-6</vendor-number><on-hold type="boolean">false</on-hold><language-code>E</language-code><purchase-organization-code>5100</purchase-organization-code><group-key>1234</group-key><vendor-partner-type-code>Z4</vendor-partner-type-code><currency-code>EUR</currency-code><primary-address><street1>@@@@ JIU BI VILLAGE</street1><street2>77774</street2><city>GUANGZHOU CITY</city><state>190-Guangdong</state><postal-code>511480</postal-code><country><code>CN</code></country></primary-address><payment-term><code>9999</code></payment-term><content-groups><content-group><name>730_Company Code</name></content-group></content-groups><po-method>prompt</po-method><invoice-matching-level>3-way</invoice-matching-level></supplier> 

任何想法?

回答

0

对于该任务,您在<param>中的内容必须包装为<![CDATA[ .. ]]>。否则,它将成为根XML的一部分,并失去它作为XPath结果的宽松数据的目的。