【问题标题】:How do I retrieve XML remotely and output it as JSON?如何远程检索 XML 并将其输出为 JSON?
【发布时间】:2014-02-15 14:42:51
【问题描述】:

我对 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 的响应。我尝试将响应转换为字符串并使用它,但这也不起作用。

最好的方法是什么?

【问题讨论】:

  • 看看这个。这解释了谁将 XML 转换为 JSON。 stackoverflow.com/questions/20722533/…
  • 完美。这正是我一直在寻找的。您介意将其发布为答案以便我接受吗?
  • 很高兴它对你有用。我已经发表了我的评论作为答案。谢谢!

标签: mule mule-studio


【解决方案1】:

看看这个。这解释了谁将 XML 转换为 JSON。

Mule: Is there simple way of converting XML to JSON

【讨论】:

    猜你喜欢
    • 2023-03-10
    • 1970-01-01
    • 2018-09-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-09-08
    • 1970-01-01
    相关资源
    最近更新 更多