Mule ESB 3.6 Object To XML not working correctly a

2019-06-06 22:50发布

I'm new to Mule ESB (3.6) and was testing out a simple flow to retrieve records from a MS SQL Server Express DB and convert results to XML using the 'Object to XML' Transformer.

The 'Customer' table only has 2 rows and my query is just returning all fields in the DB.

My problem: the XML returned is not what I expected (mapped based on a linked-list and not the XML structure based on the query). If I replace the transformer with a 'Object to JSON' version, the JSON returned represents the query results only (which is what I would expect).

I have followed a number of online tutorials and in all there is no mention of any extra settings/values to apply to the Object to XML transformer (so assuming the defaults apply).

Is this a bug or do I need to apply more settings somewhere?

XML of my Flow

<?xml version="1.0" encoding="UTF-8"?>
<mule xmlns:json="http://www.mulesoft.org/schema/mule/json" xmlns:data-mapper="http://www.mulesoft.org/schema/mule/ee/data-mapper" xmlns:file="http://www.mulesoft.org/schema/mule/file" xmlns:mulexml="http://www.mulesoft.org/schema/mule/xml" xmlns:http="http://www.mulesoft.org/schema/mule/http" xmlns:db="http://www.mulesoft.org/schema/mule/db" 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.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/http http://www.mulesoft.org/schema/mule/http/current/mule-http.xsd
http://www.mulesoft.org/schema/mule/db http://www.mulesoft.org/schema/mule/db/current/mule-db.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://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/json http://www.mulesoft.org/schema/mule/json/current/mule-json.xsd">
<http:listener-config name="HTTP_Listener_Configuration" host="0.0.0.0" port="8081" basePath="/DBQuery" doc:name="HTTP Listener Configuration"/>
    <db:generic-config name="Generic_Database_Configuration" url="jdbc:sqlserver://xxxx;databaseName=xxxx;user=xxxx;password=xxxx" doc:name="Generic Database Configuration" driverClassName="com.microsoft.sqlserver.jdbc.SQLServerDriver"/>
<file:connector name="File" outputPattern="#[server.dateTime.format(&quot;yyyyMMdd'T'HHmmss.SSS&quot;)].xml" autoDelete="true" streaming="true" validateConnections="true" doc:name="File"/>
<flow name="dbqueryFlow">
    <http:listener config-ref="HTTP_Listener_Configuration" path="/" doc:name="HTTP"/>
    <db:select config-ref="Generic_Database_Configuration" doc:name="Database ">
<db:parameterized-query><![CDATA[SELECT CustomerNumber
  ,CustomerName
  ,CustomerAddressLine1
  ,CustomerAddressLine2
  ,CustomerSuburb
  ,CustomerStateName
  ,CustomerPostcode
  FROM Customer]]>
</db:parameterized-query>
</db:select>
    <mulexml:object-to-xml-transformer encoding="UTF-8" mimeType="text/xml" doc:name="Object to XML"/>
    <file:outbound-endpoint path="C:\Mule\DBQuery\Backup" connector-ref="File" responseTimeout="10000" doc:name="File"/>
    <logger level="INFO" doc:name="Logger" message="#[payload]"/>
</flow>

XML returned

<linked-list>
  <org.mule.util.CaseInsensitiveHashMap serialization="custom">
    <unserializable-parents/>
    <org.mule.util.CaseInsensitiveHashMap>
      <default/>
      <float>0.75</float>
      <int>16</int>
      <int>7</int>
      <string>CustomerNumber</string>
      <string>1                                       </string>
      <string>CustomerSuburb</string>
      <string>Mt Eliza                                </string>
      <string>CustomerStateName</string>
      <string>Victoria                                </string>
      <string>CustomerAddressLine1</string>
      <string>Street name                                                                                         </string>
      <string>CustomerPostcode</string>
      <string>3930      </string>
      <string>CustomerAddressLine2</string>
      <string>                                                                                                    </string>
      <string>CustomerName</string>
      <string>Sarge                                                                                               </string>
    </org.mule.util.CaseInsensitiveHashMap>
  </org.mule.util.CaseInsensitiveHashMap>
  <org.mule.util.CaseInsensitiveHashMap serialization="custom">
    <unserializable-parents/>
    <org.mule.util.CaseInsensitiveHashMap>
      <default/>
      <float>0.75</float>
      <int>16</int>
      <int>7</int>
      <string>CustomerNumber</string>
      <string>2                                       </string>
      <string>CustomerSuburb</string>
      <string>Balwyn                                  </string>
      <string>CustomerStateName</string>
      <string>Victoria                                </string>
      <string>CustomerAddressLine1</string>
      <string>Street     name                                                                                         </string>
      <string>CustomerPostcode</string>
      <string>3920      </string>
      <string>CustomerAddressLine2</string>
      <string>                                                                                                    </string>
      <string>CustomerName</string>
      <string>Daniel                                                                                              </string>
    </org.mule.util.CaseInsensitiveHashMap>
  </org.mule.util.CaseInsensitiveHashMap>
</linked-list>

JSON returned (DB fields only - which is what I expected)

[{"CustomerNumber":"1                                             ","CustomerSuburb":"Mt Eliza                                ","CustomerStateName":"Victoria                                ","CustomerAddressLine1":"Street name                                                                                         ","CustomerPostcode":"3930      ","CustomerAddressLine2":"                                                                                                    ","CustomerName":"Sarge                                                                                               "},{"CustomerNumber":"2                                       ","CustomerSuburb":"Balwyn                                  ","CustomerStateName":"Victoria                                ","CustomerAddressLine1":"Street name                                                                                         ","CustomerPostcode":"3920      ","CustomerAddressLine2":"                                                                                                    ","CustomerName":"Daniel                                                                                              "}]

Also how do you remove the trailing white spaces from the output?

Thanks David

3条回答
看我几分像从前
2楼-- · 2019-06-06 23:23

One option can be use custom transformers.

There are some ways to write custom transformer. In the code below I am using extending from AbstractTransformer which provide doTransform method with 2 params. First the source and the second one is the encoding. If you want to manipulate data from previous step in your flow use src

Custom Transformer:

public class ContactsBundleTransformer extends AbstractTransformer {

    @SuppressWarnings("unchecked")
    @Override
    protected Object doTransform(Object src, String enc)
            throws TransformerException {    
        return null;
    }

}

Register custom transformer, in to order to make transformer available in your flows:

<spring:beans>
    <spring:bean id="contactsBundleTransformer" name="contactsBundleTransformer" class="io.eddumelendez.xml.ContactsBundleTransformer"/>
</spring:beans>

In your flow you have to refence to the custom transformer. You can see the sample below.

Flow:

<flow name="demoFlow">
        <http:listener config-ref="HTTP_Listener_Configuration" path="/" doc:name="HTTP"/>
        <db:select config-ref="MySQL_Configuration" doc:name="Database" >
            <db:parameterized-query><![CDATA[select node_id, bundle_data from pm_contacts_bundle]]></db:parameterized-query>
        </db:select>
        <logger message="#[payload]" level="INFO" doc:name="Logger"/>
        <transformer ref="contactsBundleTransformer" doc:name="Transformer Reference"/>
        <mulexml:object-to-xml-transformer doc:name="Object to XML"/>
        <logger message="#[payload]" level="INFO" doc:name="Logger"/>

</flow>
查看更多
倾城 Initia
3楼-- · 2019-06-06 23:28

Success!

After a lot of mucking around (and a crash course in learning XML with Java) and taking Eddu's post above, I was able to create a custom transformer.

Changed the 'Object to XML' transformer to a 'Java' transformer in my flow. Created Class SQLCustomerToXML (ensuring return type of String).

Transformer

import java.io.StringWriter;
import java.util.List;
import java.util.Map;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import org.mule.api.transformer.TransformerException;
import org.mule.transformer.AbstractTransformer;
import org.w3c.dom.Document;
import org.w3c.dom.Element;

public class SQLCustomerToXML extends AbstractTransformer {

    @SuppressWarnings("unchecked")
    @Override
    protected Object doTransform(Object src, String enc)
        throws TransformerException {

    List<Map<String, Object>> list = (List<Map<String, Object>>) src;
    Document doc;

    try {

        DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
        DocumentBuilder docBuilder = docFactory.newDocumentBuilder();

        // root elements
        doc = docBuilder.newDocument();
        Element rootElement = doc.createElement("Customers");
        doc.appendChild(rootElement);

        for (Map<String, Object> map : list) {
            // New Customer Entry
            logger.info("Customer:");
            Element customerElement = doc.createElement("Customer");
            rootElement.appendChild(customerElement);

            // Each entry in Map for Customer
            for (Map.Entry<String, Object> element : map.entrySet()) {
                logger.info("   " + element.getKey() + ":" + element.getValue().toString());
                Element e = doc.createElement(element.getKey());
                e.setTextContent(element.getValue().toString());
                customerElement.appendChild(e);
            }
        }

        TransformerFactory transformerFactory = TransformerFactory.newInstance();
        Transformer transformer = transformerFactory.newTransformer();
        DOMSource source = new DOMSource(doc);

        // Write XML to String
        StringWriter writer = new StringWriter();
        StreamResult result = new StreamResult(writer);
        transformer.transform(source, result);

        logger.info("XML : " + writer.toString());

        return writer.toString();

      } catch (ParserConfigurationException pce) {
        pce.printStackTrace();
      } catch (Exception e) {
        e.printStackTrace();
      }

    return null;
    }
}

Java Transformer settings

<custom-transformer returnClass="java.lang.String" encoding="UTF-8" mimeType="text/xml" class="SQLCustomerToXML" doc:name="Java"/> 

XML Result (in file as well as in browser)

<?xml version="1.0" encoding="UTF-8"?>
<Customers>
    <Customer>
        <CustomerNumber>1</CustomerNumber>
        <CustomerSuburb>Mt Eliza</CustomerSuburb>
        <CustomerStateName>Victoria</CustomerStateName>
        <CustomerAddressLine1>Street name</CustomerAddressLine1>
        <CustomerPostcode>3930</CustomerPostcode>
        <CustomerAddressLine2 />
        <CustomerName>Sarge</CustomerName>
    </Customer>
    <Customer>
        <CustomerNumber>2</CustomerNumber>
        <CustomerSuburb>Balwyn</CustomerSuburb>
        <CustomerStateName>Victoria</CustomerStateName>
        <CustomerAddressLine1>Street name</CustomerAddressLine1>
        <CustomerPostcode>3920</CustomerPostcode>
        <CustomerAddressLine2 />
        <CustomerName>Daniel</CustomerName>
    </Customer>
</Customers>

David

查看更多
放我归山
4楼-- · 2019-06-06 23:44

Thanks David McInnes, you saved my day. A small improvement in code for handling Null exception in e.setTextContent().

import java.io.StringWriter;
import java.util.List;
import java.util.Map;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource; 
import javax.xml.transform.stream.StreamResult;
import org.mule.api.transformer.TransformerException;
import org.mule.transformer.AbstractTransformer;
import org.w3c.dom.Document;
import org.w3c.dom.Element;

public class SQLCustomerToXML extends AbstractTransformer {

@SuppressWarnings("unchecked")
@Override
protected Object doTransform(Object src, String enc)
    throws TransformerException {

List<Map<String, Object>> list = (List<Map<String, Object>>) src;
Document doc;

try {

    DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
    DocumentBuilder docBuilder = docFactory.newDocumentBuilder();

    // root elements
    doc = docBuilder.newDocument();
    Element rootElement = doc.createElement("Customers");
    doc.appendChild(rootElement);

    for (Map<String, Object> map : list) {
        // New Customer Entry
        logger.info("Customer:");
        Element customerElement = doc.createElement("Customer");
        rootElement.appendChild(customerElement);

        // Each entry in Map for Customer
        for (Map.Entry<String, Object> element : map.entrySet()) {
           //  logger.info("   " + element.getKey() + ":" + element.getValue().toString());  // This will throw error if value is null
            Element e = doc.createElement(element.getKey());
            if (element.getValue() != null) {
                e.setTextContent(element.getValue().toString());
            } else {
                e.setTextContent("null");
            }   
            e.setTextContent(element.getValue().toString());
            customerElement.appendChild(e);
        }
    }

    TransformerFactory transformerFactory = TransformerFactory.newInstance();
    Transformer transformer = transformerFactory.newTransformer();
    DOMSource source = new DOMSource(doc);

    // Write XML to String
    StringWriter writer = new StringWriter();
    StreamResult result = new StreamResult(writer);
    transformer.transform(source, result);

    logger.info("XML : " + writer.toString());

    return writer.toString();

  } catch (ParserConfigurationException pce) {
    pce.printStackTrace();
  } catch (Exception e) {
    e.printStackTrace();
  }

return null;
}   }
查看更多
登录 后发表回答