【问题标题】:XML to POJO MappingXML to POJO Mapping
【发布时间】:2014-05-26 16:41:35
【问题描述】:

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

  1. 接收不同的 XML 请求
  2. 将它们转换为 JIBX 生成的 Java 对象
  3. 将 JIBX 生成的 Java 对象映射到 POJO 中
  4. 将 POJO 发送到另一个服务
  5. 返回一个 POJO 响应
  6. 将 POJO 映射回 JIBX 生成的 Java 对象
  7. 将 JIBX 生成的 Java 对象转换回 XML
  8. 将 XML 返回给客户端。

我想让这个过程更有效率。谁能建议如何? JIBX 可以直接映射到我的 POJO 中吗?

【问题讨论】:

    标签: java xml jibx


    【解决方案1】:

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

    Jibx Introduction

    【讨论】:

      【解决方案2】:

      在此您需要在 cmets 中注释的 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;
              }
      
          }
      

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

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

      【讨论】:

      • 使用上面的 Lonk 下载库
      • 能否请您详细说明您的答案,添加更多关于您提供的解决方案的描述?
      • Can U Please Use the link to download code 也许你会得到清晰的代码视图。
      【解决方案3】:

      String(XML in String) 到列表

      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.测试--main方法

      <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>
      

      【讨论】:

      • 输出:FolderItems [folderItem=[FolderItem [itemId=480004439, itemName=Lapse_Invoice_AI, itemType=application/x-thunderhead-ddv, description=附加利息/附加被保险人的失效通知发票, tags=[ ], path=/Templates/Billing Center/Lapse_Invoice_AI]]]
      猜你喜欢
      • 2011-07-08
      • 1970-01-01
      • 2023-03-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-05-30
      • 2013-09-11
      相关资源
      最近更新 更多