如何远程检索XML并将其作为JSON输出?

时间:2014-01-24 14:44:14

标签: mule mule-studio

我对Mule完全不熟悉,我正在尝试组建一个系统来从远程服务器检索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 个答案:

答案 0 :(得分:1)

检查一下。这解释了将XML转换为JSON的人员。

Mule: Is there simple way of converting XML to JSON