有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java两种不同的模式和JAXB封送器

假设我们有带模式的XML consnensual和带一些公共字段的Java类:

    <objectFromSchema1>
     <element1/>
     <commonElement1/>
     <commonElement2/>
     <element2/>
    </objectFromSchema1>

   public class X {
     private String element1;
     private String commonElement1;
     private String commonElement2;
     private String element2;
   }

将此类XML解组为Java对象是一种很好的方法吗?这意味着:转换所有一致同意的字段,并在rest上设置null


共 (2) 个答案

  1. # 1 楼答案

    “是的”

    如果你有一个xsd,你也可以通过<artifactId>maven-jaxb2-plugin</artifactId>maven插件自动生成这些类

    你们班的一个例子

    import java.io.Serializable;
    
    import javax.xml.bind.annotation.XmlAccessType;
    import javax.xml.bind.annotation.XmlAccessorType;
    import javax.xml.bind.annotation.XmlRootElement;
    import javax.xml.bind.annotation.XmlType;
    
    @XmlAccessorType(XmlAccessType.FIELD)
    @XmlType(name = "objectFromSchema1", propOrder = {
    
    })
    @XmlRootElement(name = "objectFromSchema1")
    public class ObjectFromSchema1
    implements Serializable
    {
    
        private final static long serialVersionUID = 12343L;
        protected String element1;
        protected String element2;
        protected String commonElement1;
        protected String commonElement2;
        public String getElement1() {
            return element1;
        }
        public void setElement1(String element1) {
            this.element1 = element1;
        }
        public String getElement2() {
            return element2;
        }
        public void setElement2(String element2) {
            this.element2 = element2;
        }
        public String getCommonElement1() {
            return commonElement1;
        }
        public void setCommonElement1(String commonElement1) {
            this.commonElement1 = commonElement1;
        }
        public String getCommonElement2() {
            return commonElement2;
        }
        public void setCommonElement2(String commonElement2) {
            this.commonElement2 = commonElement2;
        }
    
    }
    

    使用它的主要方法

    public static void main(String[] args) throws JAXBException {
            final JAXBContext context = JAXBContext.newInstance(ObjectFromSchema1.class);
            final Marshaller m = context.createMarshaller();
            m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
    
            final ObjectFromSchema1 objectFromSchema1 = new ObjectFromSchema1();
    
            objectFromSchema1.setCommonElement1("commonElement1");
            objectFromSchema1.setCommonElement2("commonElement2");
            objectFromSchema1.setElement1("element1");
            objectFromSchema1.setElement2("element2");
    
            m.marshal(objectFromSchema1, System.out);
        }
    

    输出

    <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
    <objectFromSchema1>
        <element1>element1</element1>
        <element2>element2</element2>
        <commonElement1>commonElement1</commonElement1>
        <commonElement2>commonElement2</commonElement2>
    </objectFromSchema1>