XML到POJO映射

时间:2014-05-26 16:41:35

标签: java xml jibx

我有一项服务可以执行以下操作:

  1. 接收不同的XML请求
  2. 将它们转换为JIBX生成的Java对象
  3. 将JIBX生成的Java对象映射到POJO
  4. 将POJO发送到另一个服务
  5. 获得POJO回复
  6. 将POJO映射回JIBX生成的Java对象
  7. 将JIBX生成的Java对象转换回XML
  8. 将XML返回给客户端。
  9. 我想让这个过程更有效率。有谁能建议怎么样? JIBX可以直接映射到我的POJO吗?

3 个答案:

答案 0 :(得分:0)

是的Jibx可以使用Jibx映射文件直接映射到您的POJO。我认为以下链接对于理解Jibx绑定非常有帮助。

Jibx Introduction

答案 1 :(得分:0)

在这个你需要的库中,可以在评论中评论的网址(4shared.com)中找到。

 package com.xml.Sample.MainP;

    import java.io.File;
    import java.util.List;

    import javax.xml.parsers.DocumentBuilder;
    import javax.xml.parsers.DocumentBuilderFactory;

    import org.w3c.dom.Document;

    import com.xml.Sample.Actions.XMLAction;
    import com.xml.Sample.Model.States;

    public class Retrieve {
        public static String XMLModelName = "com.xml.Sample.Model.States";
        private static String cities = "E:\\Webeclipseworkspace\\Samples\\src\\Srates.xml";

        public static void main(String[] args) {
            try {
                DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
                DocumentBuilder db = dbf.newDocumentBuilder();
                File f = new File(cities);
                Document doc = db.parse(f);
                doc.getDocumentElement().normalize();
                XMLAction xmla = new XMLAction();
                List<States> listXML = xmla.readData(XMLModelName, doc);
                // System.out.println(listXML);
                String xmlData = xmla.writtingData(listXML);
                System.out.println(xmlData);

            } catch (Exception e) {
                // TODO: handle exception
                System.out.println(e);
            }
        }

    }



    package com.xml.Sample.Model;

    import com.xml.Sample.XMLAnn.XMLColumn;
    import com.xml.Sample.XMLAnn.XMLReport;

    @XMLReport(reportName = "row")
    public class Directory {

        private String city_id;
        private String city_name;
        private String state_id;

        @XMLColumn(label = "city_id")
        public String getCity_id() {
            return city_id;
        }

        public void setCity_id(String city_id) {
            this.city_id = city_id;
        }

        @XMLColumn(label = "city_name")
        public String getCity_name() {
            return city_name;
        }

        public void setCity_name(String city_name) {
            this.city_name = city_name;
        }

        @XMLColumn(label = "state_id")
        public String getState_id() {
            return state_id;
        }

        public void setState_id(String state_id) {
            this.state_id = state_id;
        }

    }

这里我创建了自己的库,用于将Pojo类转换为xml和xml到pojo类。

在评论下使用以下链接(4Shared.com)下载库以添加以上代码。

答案 2 :(得分:-1)

字符串(字符串中的XML)到列表

<强> 1。 FolderItem.java

<code>
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
@XmlAccessorType(XmlAccessType.FIELD)
public class FolderItem {
    long itemId ;
       String itemName; 
       String itemType;
       String description;
       String[] tags;
       String path;
/* setters and getters 
   Annotations not required*/
}
</code>

<强> 2。 FolderItems.java

<code>
import java.util.List;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
public class FolderItems {
    @XmlElement
    private List<FolderItem> folderItem;
/* setter and getter */
}
</code>

第3。测试 - 主要方法

<code>
class Test{
public static void main(String[] args) throws Exception {
   FolderItems f = (FolderItems)strToVo(content, FolderItems.class);
   System.out.println(f);
}
static Object strToVo(String content, Class c) throws JAXBException {
        JAXBContext jc = JAXBContext.newInstance(c);
        Unmarshaller unmarshaller = jc.createUnmarshaller();
        return unmarshaller.unmarshal(new InputSource(new StringReader(content)));
    }
}
</code>

<强> 4。字符串中的XML

<code>

<?xml version='1.0' encoding='UTF-8' standalone='yes'?>
<FolderItems>
    <folderItem>
        <description>Lapse notice invoice for additional interests/additional insureds</description>
        <itemId>480004439</itemId>
        <itemName>Lapse_Invoice_AI</itemName>
        <itemType>application/x-thunderhead-ddv</itemType>
        <path>/Templates/Billing Center/Lapse_Invoice_AI</path>
        <tags></tags>
    </folderItem>
</FolderItems>
</code>