将POJO转换为XML

8
我需要进行以下SOAP POST请求:
POST /sample/demo.asmx HTTP/1.1
Host: www.website.org
Content-Type: text/xml; charset=utf-8
Content-Length: length
SOAPAction: "https://www.website.org/Method"

<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
  <soap:Body>
    <Method xmlns="https://www.ourvmc.org/">
      <item1>string</item1>
      <item2>string</item2>
      <item3>string</item3>
      <item4>string</item4>
    </Method>
  </soap:Body>
</soap:Envelope>

我已经达到了

POST /sample/demo.asmx HTTP/1.1
Host: www.website.org
Content-Type: text/xml; charset=utf-8
Content-Length: length
SOAPAction: "https://www.website.org/Method"

<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Body>
<Method xmlns="http://tempuri.org/"/>
</soap:Body>
</soap:Envelope>

我有一个名为Method的POJO类,其中包含item1、item2、item3、item4的getter和setter方法。 我需要将该POJO转换为XML格式,即

<item1>string</item1>
  <item2>string</item2>
  <item3>string</item3>
  <item4>string</item4>

然后进行POST请求。有人能提供如何实现吗?我已经做了研究,但没有找到有用的解决方案。


你可以看一下FasterXML的Jackson Dataformat XML。这是链接:https://github.com/FasterXML/jackson-dataformat-xml - user2004685
我在很多解决方案中看到了FasterXML,但是没有在任何地方找到一行代码。你能指导我一个链接,在那里我可以找到POJO转XML的方法吗? - Logic
这里有一个:https://dev59.com/oWUq5IYBdhLWcg3wMNgK - user2004685
@user2004685 我需要将XML转换为字符串而不是文件。 - Logic
http://javatechig.com/android/how-to-convert-pojo-to-xml-in-android - ak sacha
@Logic 你可以使用 StringWriter 替代文件。 - user2004685
2个回答

4
请使用以下的XMLUtil将普通Java对象转换成xml。
用法示例:
1)假设要转换的Java对象是Student。
@XmlRootElement(name = "Student")
@XmlAccessorType(XmlAccessType.FIELD)
public class Student {

    @XmlAttribute
    private String type;

    @XmlElement(name="Name")
    private String name;

    public void setType(String type) {
        this.type = type;
    }

    public void setName(String name) {
        this.name = name;
    }
 }

2) XMLUtil

import java.io.StringWriter;    
import java.util.logging.Logger;    
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;


public class XMLUtil {


    public static String toXML(Object data) {
        String xml = "";
        try {
            LOGGER.info("Generating xml for: " + data.getClass());
            JAXBContext jaxbContext = JAXBContext.newInstance(data.getClass());
            Marshaller jaxbMarshaller = jaxbContext.createMarshaller();
            jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
            //jaxbMarshaller.marshal(data, System.out);
            StringWriter sw = new StringWriter();
            jaxbMarshaller.marshal(data, sw);
            xml = sw.toString();
        } catch (JAXBException e) {
            //handle your exception here
        }
        return xml;
    }

}

3) 将数据设置到学生对象中并传递给工具类

   Student st = new Student();
   st.setType("schoolStudent");
   st.setName("Devendra");

   String studentXml = XMLUtil.toXML(st);

0

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接