2014-01-24 36 views
1

我對Mule完全陌生,我試圖將系統放在一起從遠程服務器檢索XML文件,並將其顯示爲原樣或將其轉換爲JSON,然後像那樣顯示它。我現在所擁有的如下:如何遠程檢索XML並將其輸出爲JSON?

<?xml version="1.0" encoding="UTF-8"?> 

<mule xmlns:tracking="http://www.mulesoft.org/schema/mule/ee/tracking" xmlns:mulexml="http://www.mulesoft.org/schema/mule/xml" xmlns:http="http://www.mulesoft.org/schema/mule/http" xmlns:json="http://www.mulesoft.org/schema/mule/json" xmlns="http://www.mulesoft.org/schema/mule/core" xmlns:doc="http://www.mulesoft.org/schema/mule/documentation" xmlns:spring="http://www.springframework.org/schema/beans" version="EE-3.4.1" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-current.xsd 
http://www.mulesoft.org/schema/mule/core http://www.mulesoft.org/schema/mule/core/current/mule.xsd 
http://www.mulesoft.org/schema/mule/json http://www.mulesoft.org/schema/mule/json/current/mule-json.xsd 
http://www.mulesoft.org/schema/mule/http http://www.mulesoft.org/schema/mule/http/current/mule-http.xsd 
http://www.mulesoft.org/schema/mule/ee/tracking http://www.mulesoft.org/schema/mule/ee/tracking/current/mule-tracking-ee.xsd 
http://www.mulesoft.org/schema/mule/xml http://www.mulesoft.org/schema/mule/xml/current/mule-xml.xsd"> 
    <flow name="TestFlow1" doc:name="TestFlow1"> 
     <http:inbound-endpoint exchange-pattern="request-response" host="localhost" port="8081" doc:name="HTTP"/> 
     <set-variable variableName="type" value="#[message.inboundProperties['type']]" doc:name="Set type variable"/> 
     <http:outbound-endpoint exchange-pattern="request-response" host="[URL REMOVED]" port="80" path="#[message.inboundProperties['feed']].php" method="GET" doc:name="HTTP" ><response><object-to-string-transformer /></response></http:outbound-endpoint> 
     <logger level="INFO" doc:name="Logger"/> 
     <choice doc:name="Choice"> 
      <when expression="#[flowVars['type']=='JSON']"> 
       <json:object-to-json-transformer doc:name="Object to JSON"/> 
      </when> 
      <when expression="#[flowVars['type']=='xml']"> 
       <mulexml:object-to-xml-transformer acceptMuleMessage="true" doc:name="Object to XML"/> 
       <logger level="INFO" doc:name="XML"/> 
      </when> 
      <otherwise> 
       <set-payload value="Type not set" doc:name="Error message"/> 
      </otherwise> 
     </choice> 
     <logger level="INFO" doc:name="Logger"/> 
    </flow> 
</mule> 

檢索XML並按原樣顯示它工作得很好。我只需要弄清楚JSON轉換。

我試着在Object-to-JSON轉換器之前放入一個HTTP-to-Object轉換器,但由於http:outbound-endpoint返回類ReleasingInputStream類的響應,會引發非法參數異常。我試過將響應轉換爲字符串,並使用該字符串,但這也不起作用。

要做到這一點,最好的方法是什麼?

+1

看看這個。這解釋了將XML轉換爲JSON的人員。 http://stackoverflow.com/questions/20722533/mule-is-there-simple-way-of-converting-xml-to-json/20755504#20755504 – user1493140

+0

完美。這正是我所期待的。 你介意張貼這個答案,以便我可以接受嗎? – Buffel

+0

很高興爲你效勞。我已經發布了我的評論作爲答案。謝謝! – user1493140

回答