-----> 解决方法见本题结尾 vvvvvvv
我正在尝试使用 Mule 编辑 XML 文件,但无法运行。
我试图开始工作的流程是:
我写的java-transform中的代码是:
package se.xcorp.mulecomponents;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.JDOMException;
import org.jdom.input.SAXBuilder;
import org.jdom.output.Format;
import org.jdom.output.XMLOutputter;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.nio.channels.FileChannel;
import org.mule.api.MuleMessage;
import org.mule.api.transformer.TransformerException;
import org.mule.api.transport.PropertyScope;
import org.mule.config.i18n.MessageFactory;
import org.mule.transformer.AbstractMessageTransformer;
public class checkXML extends AbstractMessageTransformer
{
@Override
public Object transformMessage(MuleMessage message, String outputEncoding)
throws TransformerException {
try {
SAXBuilder builder = new SAXBuilder();
//
Document doc = (Document) builder.build((java.io.InputStream)message);
Element rootNode = doc.getRootElement();
// update staff id attribute
String workpl = rootNode.getChildText("Arb_plats_ID");
if (workpl==""){
System.out.println("Empty!!!!");
rootNode.getChild("Arb_plats_ID").setText("0");
} else {
System.out.println(workpl);
}
XMLOutputter xmlOutput = new XMLOutputter();
// display nice nice
xmlOutput.setFormat(Format.getPrettyFormat());
xmlOutput.output(doc, new FileWriter("c:\\JAVATEST\\DATAUT\\1.xml"));
// xmlOutput.output(doc, System.out);
System.out.println("File updated!");
} catch (IOException io) {
io.printStackTrace();
} catch (JDOMException e) {
e.printStackTrace();
}
return null;
}
}
例外是:
++++++++++++++++++++++++++++++++++++++++++++++++++++++ ++++++++++ + 启动应用程序'xmlchecknedit' + +++++++++++++++++++++++++++++++++++ +++++++++++++++++++++++++++ 信息 2013-11-07 16:24:27,943 [[xmlchecknedit].connector.file.mule.default .receiver.01] org.mule.transport.file.FileMessageReceiver:在文件上获得锁定:C:\JAVATEST\DATAIN\40171700_100470.xml 错误 2013-11-07 16:24:28,049 [[xmlchecknedit].XMLcheckneditFlow2.stage1。 02] org.mule.exception.DefaultMessagingExceptionStrategy:
消息:org.mule.DefaultMuleMessage 无法转换为 java.io.InputStream (java.lang.ClassCastException)。消息有效负载的类型为:byte[]
代码:MULE_ERROR--2
异常堆栈是: 1. org.mule.DefaultMuleMessage 无法转换为 java.io.InputStream (java.lang.ClassCastException) se.hansandersson.mulecomponents.checkXML:36 (null) 2. org.mule.DefaultMuleMessage 无法转换为java.io.InputStream (java.lang.ClassCastException)。消息负载的类型为:byte[] (org.mule.api.transformer.TransformerMessagingException)
org.mule.transformer.AbstractTransformer:139 ( http://www.mulesoft.org/docs/site/current3/apidocs/org/mule/api/transformer/TransformerMessagingException.html )
根异常堆栈跟踪:java.lang.ClassCastException:org.mule.DefaultMuleMessage 无法在 org.mule.transformer.AbstractMessageTransformer 的 se.hansandersson.mulecomponents.checkXML.transformMessage(checkXML.java:36) 中转换为 java.io.InputStream org.mule.transformer.AbstractMessageTransformer.transform(AbstractMessageTransformer.java:93) 上的 .transform(AbstractMessageTransformer.java:145) + 3 个以上(设置调试级别日志记录或“-Dmule.verbose.exceptions=true”)
我感谢任何让这个工作的投入!
我仍然无法让它工作,我想做的是做下面的Java代码所做的事情,但把它放到一个骡子流中来处理文件管理。Java 检查 XML-element-child 是否为空,如果为空,则将其更新为“0”。
这个 java 代码的工作方式就像我不想要的那样,但我无法将它转换为 mule 流。请帮忙!/克拉斯
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.JDOMException;
import org.jdom.input.SAXBuilder;
import org.jdom.output.Format;
import org.jdom.output.XMLOutputter;
public class ModifyXMLFile {
public static void main(String[] args) {
try {
SAXBuilder builder = new SAXBuilder();
File xmlFile = new File("c:\\JAVATEST\\DATAIN\\1.xml");
Document doc = (Document) builder.build(xmlFile);
Element rootNode = doc.getRootElement();
// update staff id attribute
String workpl = rootNode.getChildText("Arb_plats_ID");
if (workpl==""){
System.out.println("Empty!!!!");
rootNode.getChild("Arb_plats_ID").setText("0");
} else {
System.out.println(workpl);
}
XMLOutputter xmlOutput = new XMLOutputter();
// display nice nice
xmlOutput.setFormat(Format.getPrettyFormat());
xmlOutput.output(doc, new FileWriter("c:\\JAVATEST\\DATAUT\\1.xml"));
// xmlOutput.output(doc, System.out);
System.out.println("File updated!");
} catch (IOException io) {
io.printStackTrace();
} catch (JDOMException e) {
e.printStackTrace();
}
}
}
解决方案!!!!
下面是 Mule XML 和 Java 类,它们一起检查 XML 元素是否为空,是否为它分配 0,然后将其传递回流以进行保存。
MULE XML-----------------------------------
<flow name="XMLcheckneditFlow2" doc:name="XMLcheckneditFlow2">
<file:inbound-endpoint path="C:\JAVATEST\DATAIN" responseTimeout="10000" doc:name="File">
</file:inbound-endpoint>
<byte-array-to-string-transformer doc:name="Byte Array to String"></byte-array-to-string-transformer>
<custom-transformer class="se.hansandersson.mulecomponents.checkXML" doc:name="Java"/>
<file:outbound-endpoint path="C:\JAVATEST\DATAUT" responseTimeout="10000" doc:name="File"/>
</flow>
</mule>
JAVA代码-------------------------------------------------------
public class checkXML extends AbstractMessageTransformer
{
@Override
public Object transformMessage(MuleMessage message, String outputEncoding)
throws TransformerException {
String returnstring="";
try {
String xmlString = (String)message.getPayload();
SAXBuilder saxBuilder = new SAXBuilder();
InputStream stream = new ByteArrayInputStream(xmlString.getBytes("UTF-8"));
Document doc = saxBuilder.build(stream);
Element rootNode = doc.getRootElement();
// update staff id attribute
String workpl = rootNode.getChildText("Arb_plats_ID");
if (workpl==""){
System.out.println("Empty!!!!");
rootNode.getChild("Arb_plats_ID").setText("0");
} else {
System.out.println(workpl);
}
XMLOutputter xmlOutput = new XMLOutputter();
// display nice nice
returnstring = xmlOutput.outputString(doc);
System.out.println("File updated!");
} catch (IOException io) {
io.printStackTrace();
} catch (JDOMException e) {
e.printStackTrace();
}
return returnstring;
}