简体   繁体   English

XML到POJO映射

[英]XML to POJO Mapping

I have a service that does the following: 我有一项服务,它可以执行以下操作:

  1. receives different XML requests 接收不同的XML请求
  2. turns them into JIBX-generated Java objects 将它们转换为JIBX生成的Java对象
  3. maps the JIBX-generated Java objects into POJOs 将JIBX生成的Java对象映射到POJO
  4. sends the POJOs to another service 将POJO发送到另一个服务
  5. gets a POJO response back 收到POJO回复
  6. maps POJO back into JIBX-generated Java objects 将POJO映射回JIBX生成的Java对象
  7. turns JIBX-generated Java objects back into XML 将JIBX生成的Java对象转换回XML
  8. returns XML to client. 将XML返回给客户端。

I'd like to make this process more efficient. 我想使这个过程更有效率。 Can anyone suggest how? 谁能建议如何? Can JIBX map directly into my POJOs? JIBX可以直接映射到我的POJO吗?

Yes Jibx can map directly to your POJOs using Jibx mapping files. 是的,Jibx可以使用Jibx映射文件直接映射到您的POJO。 I think the below link will be very helpful to understand Jibx binding. 我认为下面的链接对于理解Jibx绑定非常有帮助。

Jibx Introduction Jibx简介

In this you needed library which is available in the url(4shared.com) commented in comments. 在此,您需要一个库,该库位于注释中的url(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;
        }

    }

Here I Created Own Library For Converting Pojo classes to xml and xml to pojo classes. 在这里,我创建了自己的库,用于将Pojo类转换为xml并将xml转换为pojo类。

Use Below Link(4Shared.com) at Comments to download Library to add for The Above Code. 使用评论中的下方链接(4Shared.com)下载要为上述代码添加的库。

String(XML in String) to List 字符串(字符串中的XML)列出

1. FolderItem.java 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 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. Testing--main method 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 in String 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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM