将 http 响应转换为 xml

Transforming http response to xml

这些是我使用 Mule 的第一步,所以我可能不了解一些基本概念。

有一家商店 运行 Prestashop,公开了 REST 服务。

我现在想做的就是发送请求到存储,获得响应,替换一个值,并将其保存到文件中。

响应如下所示:

<?xml version="1.0" encoding="UTF-8"?>
<prestashop xmlns:xlink="http://www.w3.org/1999/xlink">
<stock_available>
    <id><![CDATA[1]]></id>
    <id_product xlink:href="http://xxxxxxxxxxxxx/api/products/1"><![CDATA[1]]></id_product>
    <id_product_attribute><![CDATA[0]]></id_product_attribute>
    <id_shop xlink:href="http://xxxxxxxxxxxxx/api/shops/1"><![CDATA[1]]></id_shop>
    <id_shop_group><![CDATA[0]]></id_shop_group>
    <quantity><![CDATA[0]]></quantity>
    <depends_on_stock><![CDATA[0]]></depends_on_stock>
    <out_of_stock><![CDATA[2]]></out_of_stock>
</stock_available>
</prestashop>

我想替换值 'quantity'。现在 - 具有恒定值。 我目前的流程是:

<?xml version="1.0" encoding="UTF-8"?>
<mule xmlns:file="http://www.mulesoft.org/schema/mule/file" xmlns:data-mapper="http://www.mulesoft.org/schema/mule/ee/data-mapper" xmlns:http="http://www.mulesoft.org/schema/mule/http" xmlns:mulexml="http://www.mulesoft.org/schema/mule/xml" 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.6.0"
    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/http http://www.mulesoft.org/schema/mule/http/current/mule-http.xsd
http://www.mulesoft.org/schema/mule/xml http://www.mulesoft.org/schema/mule/xml/current/mule-xml.xsd
http://www.mulesoft.org/schema/mule/file http://www.mulesoft.org/schema/mule/file/current/mule-file.xsd
http://www.mulesoft.org/schema/mule/ee/data-mapper http://www.mulesoft.org/schema/mule/ee/data-mapper/current/mule-data-mapper.xsd">
    <http:listener-config name="HTTP_Listener_Configuration" host="0.0.0.0" port="8081" doc:name="HTTP Listener Configuration"/>
    <http:request-config name="HTTP_Request_Configuration" host="xxxxxxxxxxxxxxxx" port="80" basePath="/api/" doc:name="HTTP Request Configuration">
        <http:basic-authentication username="xxxxxxxxxxxxxxxxxxxxxxx" password="x"/>
        <http:raml-api-configuration location="api.raml"/>
    </http:request-config>
    <flow name="1Flow">
        <http:listener config-ref="HTTP_Listener_Configuration" path="/" doc:name="HTTP"/>
        <http:request config-ref="HTTP_Request_Configuration" path="/stock_availables/{id}" method="GET" doc:name="HTTP">
            <http:request-builder>
                <http:uri-param paramName="id" value="#[message.inboundProperties.'http.query.params'.id]"/>
            </http:request-builder>
        </http:request>
        <byte-array-to-object-transformer doc:name="Byte Array to Object" mimeType="text/xml"/>
        <mulexml:object-to-xml-transformer doc:name="Object to XML"/>
        <file:outbound-endpoint path="C:\test" outputPattern="test.txt" responseTimeout="10000" doc:name="File"/>
    </flow>
</mule>

这已经连接到服务,获取响应并保存文件。 但是我不知道如何修改它的内容。

我无法理解的问题之一是: 当我查看调试器时,在 "Byte Array To Object" 之后,有效载荷的类型为 java.lang.string。在 "Object to XML" 之后,有效载荷的类型仍然是 java.lang.string,有效载荷的值为:

<string>&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;
&lt;prestashop xmlns:xlink=&quot;http://www.w3.org/1999/xlink&quot;&gt;
&lt;stock_available&gt;..............blahblahblah.........</string>

当我尝试使用 xslt 转换时出现错误。

如何修改该值?

您可以通过 xpath 从响应 xml 中获取所有值。 查看以下 link 中的示例 5 http://www.mulesoft.org/documentation/display/current/Mule+Expression+Language+Examples

我只是想给你一个大致的方向,不一定是给你一个直接的解决方案,很抱歉!

描述您需要在您的,更具体地说是 Transformer 中引入 "Message Processor" 的关键字。

Transformer 将收到您的 XML,将对其进行所需的调整并将修改后的 XML 提供给输出,在您的情况下为文件端点。

例如,您可以以 POJO 的形式编写转换器。您将 XML 作为字符串发送给它。您 Java class 有一个方法可以进行处理和 returns 修改后的字符串(也就是您想要的 XML)。

要获取字符串,您应该使用字节数组到字符串的转换器。

这里有几个选项。首先,你的 http 传输将 return 你一个字节数组,然后你可以使用字节数组到字符串转换器将其转换为一个字符串。之后,您可以调用自定义处理器并在 java 代码中转换字符串有效负载,或者尝试将 MEL 中的正则表达式函数 (#[regex()]) 与字符串附加转换器进行某种组合以完全转换有效负载在 mule xml 代码中。