2012-07-09 87 views
0

是否有像GSON这样的自由开源的SOAP解析器,可以自动将解析的数据映射到相应的bean?如果没有,您能推荐一个好的但免费且开源的SOAP解析器吗?像GSON这样的SOAP解析器?

感谢先进!

+0

你试过'DOM' ,'SAX'? – adatapost 2012-07-09 02:45:50

+0

是的。但是它没有类似于Json的东西,其中bean的字段自动映射到bean。 – Arci 2012-07-09 02:51:21

+1

要使用SOAP,您应该使用Web服务工具包(如JAX-WS)。这是一个相当重量级的解决方案,但该工具包应该能够为您生成大部分支持代码。 – millimoose 2012-07-09 07:07:26

回答

4

您可以尝试JAXB(XML绑定的Java架构)API。看看样品:

xml文件

<?xml version="1.0" encoding="UTF-8"?> 
<Root> 
    <Child> 
     <foo>10</foo> 
    </Child> 
    <Child> 
     <foo>20</foo> 
    </Child> 
</Root> 

的Java的POJO表示XML架构

@XmlRootElement(name="Root") 
class Bar{ 
    private List<Foo> list=new ArrayList(); 
    @XmlElement(name="Child") 
    public List<Foo> getList(){ return list;} 
} 
class Foo{ 
    private Integer foo; 
    public Foo(){ foo=0;} 
    public Foo(Integer foo) { this.foo=foo;} 
    public Integer getFoo() { return foo; } 
    public void setFoo(Integer foo){ this.foo=foo;} 
} 

来读取XML的Java对象

JAXBContext context=JAXBContext.newInstance(Bar.class); 
Unmarshaller um=context.createUnmarshaller(); 

Bar bar=(Bar)um.unmarshal(new File("x:\\path\\xmldoc.xml")); // you may specify the URL too. 

System.out.println(bar.getList()); 
for(Foo c:bar.getList()){ 
    System.out.println(c.getFoo()); 
} 
+0

谢谢!我得到它的工作!自从我使用JDK 1.5以来,我现在使用JAXB并下载了所需的jar。顺便说一下,你还可以回答我的其他问题(这是关于JAXB):http://stackoverflow.com/questions/11425109/how-to-include-the-soap-envelope-tag-when-marshalling-using-jaxb ? – Arci 2012-07-11 03:29:50