2016-03-04 45 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結果的寬鬆數據的目的。