我想从下面的肥皂响应中获取AddResult的值,即100:
<?xml version="1.0" encoding="utf-8"?><soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"><soap:Body><AddResponse xmlns="http://tempuri.org/"><AddResult>100</AddResult></AddResponse></soap:Body></soap:Envelope>
下面是Java代码:
package com.learncamel.routes.xmlxstream;
import java.util.HashMap;
import java.util.Map;
import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.dataformat.xstream.XStreamDataFormat;
import com.learncamel.domain.AddResponse;
public class XmlResponseToObject extends RouteBuilder{
@Override
public void configure() throws Exception {
// TODO Auto-generated method stub
Map<String, String> reference = new HashMap<String, String>();
reference.put("AddResponse ", AddResponse.class.getName());
XStreamDataFormat xstreamDataFormat = new XStreamDataFormat();
xstreamDataFormat.setAliases(reference);
xstreamDataFormat.setPermissions(AddResponse.class.getName());
try {
from("direct:xmlinput")
.log("Received Message before unmarshal is ${body} and Headers are ${headers}")
.unmarshal(xstreamDataFormat)
.log("Received Message after unmarshal is ${body} and Headers are ${headers}")
.to("log:?level=INFO&showBody=true");
} catch(Exception e) {
e.printStackTrace();
}
}
}
下面是junit代码:
package com.learncamel.routes.xmlxstream;
import org.apache.camel.RoutesBuilder;
import org.apache.camel.test.junit4.CamelTestSupport;
import org.junit.Test;
public class XmlResponseToObjectTest extends CamelTestSupport {
@Override
public RoutesBuilder createRouteBuilder() throws Exception {
return new XmlResponseToObject();
}
@Test
public void unMarshalTest() throws InterruptedException{
//AddResponse addresponse=new AddResponse();
String input="<?xml version=\"1.0\" encoding=\"utf-8\"?><soap:Envelope xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\"><soap:Body><AddResponse xmlns=\"http://tempuri.org/\"><AddResult>100</AddResult></AddResponse></soap:Body></soap:Envelope>";
template.sendBody("direct:xmlinput",input);
}
}
下面是POJO:
package com.learncamel.domain;
public class AddResponse {
private String AddResult;
public String getAddResult() {
return AddResult;
}
public void setAddResult(String addResult) {
AddResult = addResult;
}
}
com.thoughtworks.xstream.mapper.CannotResolveClassException:信封 在 com.thoughtworks.xstream.mapper.DefaultMapper.realClass(DefaultMapper.java:81) 在 com.thoughtworks.xstream.mapper.MapperWrapper.realClass(MapperWrapper.java:125) 在 com.thoughtworks.xstream.mapper.DynamicProxyMapper.realClass(DynamicProxyMapper.java:55) 在 com.thoughtworks.xstream.mapper.MapperWrapper.realClass(MapperWrapper.java:125) 在 com.thoughtworks.xstream.mapper.PackageAliasingMapper.realClass(PackageAliasingMapper.java:88) 在 com.thoughtworks.xstream.mapper.MapperWrapper.realClass(MapperWrapper.java:125) 在 com.thoughtworks.xstream.mapper.ClassAliasingMapper.realClass(ClassAliasingMapper.java:79) 在 com.thoughtworks.xstream.mapper.MapperWrapper.realClass(MapperWrapper.java:125) 在 com.thoughtworks.xstream.mapper.ArrayMapper.realClass(ArrayMapper.java:74) 在 com.thoughtworks.xstream.mapper.MapperWrapper.realClass(MapperWrapper.java:125) 在 com.thoughtworks.xstream.mapper.SecurityMapper.realClass(SecurityMapper.java:71) 在 com.thoughtworks.xstream.mapper.MapperWrapper.realClass(MapperWrapper.java:125) 在 com.thoughtworks.xstream.mapper.CachingMapper.realClass(CachingMapper.java:47) 在 com.thoughtworks.xstream.core.util.HierarchicalStreams.readClassType(HierarchicalStreams.java:29) 在 com.thoughtworks.xstream.core.TreeUnmarshaller.start(TreeUnmarshaller.java:133) 在 com.thoughtworks.xstream.core.AbstractTreeMarshallingStrategy.unmarshal(AbstractTreeMarshallingStrategy.java:32) 在com.thoughtworks.xstream.XStream.unmarshal(XStream.java:1486)在 com.thoughtworks.xstream.XStream.unmarshal(XStream.java:1454)位于 org.apache.camel.dataformat.xstream.AbstractXStreamWrapper.unmarshal(AbstractXStreamWrapper.java:374) 在 org.apache.camel.processor.UnmarshalProcessor.process(UnmarshalProcessor.java:69) 在 org.apache.camel.processor.RedeliveryErrorHandler.process(RedeliveryErrorHandler.java:548) 在 org.apache.camel.processor.CamelInternalProcessor.process(CamelInternalProcessor.java:201) 在org.apache.camel.processor.Pipeline.process(Pipeline.java:138)处 org.apache.camel.processor.Pipeline.process(Pipeline.java:101)位于 org.apache.camel.processor.CamelInternalProcessor.process(CamelInternalProcessor.java:201) 在 org.apache.camel.component.direct.DirectProducer.process(DirectProducer.java:62) 在 org.apache.camel.processor.SharedCamelInternalProcessor.process(SharedCamelInternalProcessor.java:186) 在 org.apache.camel.processor.SharedCamelInternalProcessor.process(SharedCamelInternalProcessor.java:86) 在 org.apache.camel.impl.ProducerCache $ 1.doInProducer(ProducerCache.java:541) 在 org.apache.camel.impl.ProducerCache $ 1.doInProducer(ProducerCache.java:506) 在 org.apache.camel.impl.ProducerCache.doInProducer(ProducerCache.java:369) 在 org.apache.camel.impl.ProducerCache.sendExchange(ProducerCache.java:506) 在org.apache.camel.impl.ProducerCache.send(ProducerCache.java:229) 在 org.apache.camel.impl.DefaultProducerTemplate.send(DefaultProducerTemplate.java:144) 在 org.apache.camel.impl.DefaultProducerTemplate.sendBody(DefaultProducerTemplate.java:161) 在 org.apache.camel.impl.DefaultProducerTemplate.sendBody(DefaultProducerTemplate.java:168) 在 com.learncamel.routes.xmlxstream.XmlResponseToObjectTest.unMarshalTest(XmlResponseToObjectTest.java:25) 在sun.reflect.NativeMethodAccessorImpl.invoke0(本机方法)处 sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) 在 sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 在java.lang.reflect.Method.invoke(Method.java:498)在 org.junit.runners.model.FrameworkMethod $ 1.runReflectiveCall(FrameworkMethod.java:50) 在 org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12) 在 org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47) 在 org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17) 在 org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26) 在 org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:27) 在org.junit.rules.TestWatcher $ 1.evaluate(TestWatcher.java:55)在 org.junit.rules.TestWatcher $ 1.evaluate(TestWatcher.java:55)在 org.junit.rules.RunRules.evaluate(RunRules.java:20)在 org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)在 org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78) 在 org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57) 在org.junit.runners.ParentRunner处$ 3.run(ParentRunner.java:290)在 org.junit.runners.ParentRunner $ 1.schedule(ParentRunner.java:71)在 org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)在 org.junit.runners.ParentRunner.access $ 000(ParentRunner.java:58)在 org.junit.runners.ParentRunner $ 2.evaluate(ParentRunner.java:268)在 org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:27) 在org.junit.runners.ParentRunner.run(ParentRunner.java:363)处 org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:86) 在 org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38) 在 org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:538) 在 org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:760) 在 org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:460) 在 org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:206)
答案 0 :(得分:1)
您可以尝试使用xpath从xml中提取值,例如:
.convertBodyTo(org.w3c.dom.Document.class)
.setProperty("needValue",xpath("*//AddResult/text()",String.class))
//use extracted value
如果您需要将输入xml转换为xslt组件的另一种外观。
更新:如果您确实需要使用对象来删除异常,则需要提取映射的主体AddResponse,因为您已对其进行了映射。您的输入字符串从未映射的肥皂元素“信封”开始。如果.unmarshall()之前有CxfPayload,只需将其转换为Document或String。为了成功进行测试,只需除去肥皂成分。
答案 1 :(得分:0)
public static Document loadXMLString(String response) throws Exception
{
DocumentBuilderFactory dbf =DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
InputSource is = new InputSource(new StringReader(response));
return db.parse(is);
}
public static List<String> getFullNameFromXml(String response, String tagName) throws Exception {
Document xmlDoc = loadXMLString(response);
NodeList nodeList = xmlDoc.getElementsByTagName(tagName);
List<String> ids = new ArrayList<String>(nodeList.getLength());
for(int i=0;i<nodeList.getLength(); i++) {
Node x = nodeList.item(i);
ids.add(x.getFirstChild().getNodeValue());
System.out.println(nodeList.item(i).getFirstChild().getNodeValue());
}
return ids;
}
public static void main(String arg[]) throws Exception
{
String res="<?xml version=\"1.0\" encoding=\"utf-8\"?><soap:Envelope xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\"><soap:Body><SubtractResponse xmlns=\"http://tempuri.org/\"><SubtractResult>150</SubtractResult></SubtractResponse></soap:Body></soap:Envelope>";
List<String> output = XmlResponseToObject.getFullNameFromXml(res, "SubtractResult");
String[] strarray = new String[output.size()];
output.toArray(strarray);
String x = output.get(0);
System.out.println(x);
}