我是Mule ESB(3.6)的新手,正在测试一个简单的流程,以便从MS SQL Server Express数据库中检索记录,并使用' Object to XML'将结果转换为XML。变压器。
'客户'表只有2行,我的查询只返回数据库中的所有字段。
我的问题:返回的XML不是我所期望的(基于链表而不是基于查询的XML结构映射)。如果我用“JSON”对象替换变压器'版本,返回的JSON仅代表查询结果(这是我期望的)。
我已经遵循了许多在线教程,并且没有提及要应用于Object to XML转换器的任何额外设置/值(因此假设默认值适用)。
这是一个错误还是我需要在某处应用更多设置?
我的流程的XML
<?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("yyyyMMdd'T'HHmmss.SSS")].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返回
<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返回(仅限DB字段 - 这是我的预期)
[{"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 "}]
另外,如何从输出中删除尾随空格?
由于 大卫
答案 0 :(得分:2)
成功!
经过大量的研究(以及使用Java学习XML的速成课程)以及上面的Eddu的帖子后,我能够创建一个自定义变换器。
将对象更改为XML&#39;变换器到Java&#39;我的流量变压器。 创建了类SQLCustomerToXML(确保String的返回类型)。
<强> 变压器 强>
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设置
<custom-transformer returnClass="java.lang.String" encoding="UTF-8" mimeType="text/xml" class="SQLCustomerToXML" doc:name="Java"/>
XML结果(在文件和浏览器中)
<?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>
大卫
答案 1 :(得分:1)
一个选项可以使用自定义变换器。
有一些方法可以编写自定义变换器。在下面的代码中,我使用AbstractTransformer
扩展,提供doTransform
方法和2个参数。首先是source
,第二个是encoding
。如果您想使用src
Custom Transformer:
public class ContactsBundleTransformer extends AbstractTransformer {
@SuppressWarnings("unchecked")
@Override
protected Object doTransform(Object src, String enc)
throws TransformerException {
return null;
}
}
注册自定义变压器,以便在您的流程中提供变压器:
<spring:beans>
<spring:bean id="contactsBundleTransformer" name="contactsBundleTransformer" class="io.eddumelendez.xml.ContactsBundleTransformer"/>
</spring:beans>
在您的流程中,您必须对自定义变压器进行补偿。您可以在下面看到示例。
流程:
<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>
答案 2 :(得分:0)
感谢David McInnes,你救了我的一天。 在e.setTextContent()中处理Null异常的代码的一个小改进。
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;
} }