【问题标题】:Jaxb Unmarshalling (different wrapper elements with same element)Jaxb Unmarshalling(具有相同元素的不同包装器元素)
【发布时间】:2021-08-24 06:00:15
【问题描述】:

我有以下 xml 结构:

<Response>
<Field1>
    <User>
        <Name>abc</Name>
        <Id>1</Id>  
    </User>
</Field1>   
<Field2>
    <User>
        <Name>sdf</Name>
        <Id>2</Id>  
    </User>
</Field2>   
<Field3>
    <User>
        <Name>xyz</Name>
        <Id>3</Id>  
    </User>
</Field3>
...
</Response>

有不同的 Field(X) 包装元素包含相同的 User 元素。 XML 中可以发送 n 个字段。因此,我不能为每个单独的 Jaxb。在解组步骤之后,我需要访问忽略 Field 元素的用户。不幸的是,我无权更改 xml 结构。我无法找到解决方案。任何指针都会有所帮助。

【问题讨论】:

    标签: jaxb unmarshalling


    【解决方案1】:

    你可以在 Response (root) class 上做这样的事情:

    
    @XmlRootElement(name = "Response")
    @Data
    @XmlAccessorType(XmlAccessType.FIELD)
    public class Root {
        @XmlElements(value = {
                @XmlElement(name = "Field1", type = Field.class),
                @XmlElement(name = "Field2", type = Field.class),
                @XmlElement(name = "Field3", type = Field.class)
        })
        private Field field;
    }
    

    只要遇到不同的FIelds,JAXB 就会自动映射到字段类。

    编辑/更新

    XML:

    <Response>
        <Field1>
            <Name>abc</Name>
            <Id>1</Id>
        </Field1>
        <Field2>
            <Name>sdf</Name>
            <Id>2</Id>
        </Field2>
        <Field3>
            <Name>xyz</Name>
            <Id>3</Id>
        </Field3>
    </Response>
    

    根:

    @XmlRootElement(name = "Response")
    @Data
    @XmlAccessorType(XmlAccessType.FIELD)
    public class Root {
        @XmlAnyElement
        private List<Field> action;
    }
    

    字段:

    @Data
    @XmlAccessorType(XmlAccessType.FIELD)
    public class Field {
        private String Name;
        private String Id;
    }
    
    

    主要:

    public class Main {
        public static void main(String[] args) throws JAXBException, XMLStreamException {
            final InputStream inputStream = Main.class.getClassLoader().getResourceAsStream("sample.xml");
            final XMLStreamReader xmlStreamReader = XMLInputFactory.newInstance().createXMLStreamReader(inputStream);
            final Unmarshaller unmarshaller = JAXBContext.newInstance(Root.class).createUnmarshaller();
            final Root root = unmarshaller.unmarshal(xmlStreamReader, Root.class).getValue();
            System.out.println(root.toString());
    
            Marshaller marshaller = JAXBContext.newInstance(Root.class).createMarshaller();
            marshaller.setProperty(Marshaller.JAXB_FRAGMENT, Boolean.TRUE);
            marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
            marshaller.marshal(root, System.out);
        }
    }
    

    输出:

    Root(action=[[Field1: null], [Field2: null], [Field3: null]])
    <Response>
       <Field1>
          <Name>abc</Name>
          <Id>1</Id>
       </Field1>
       <Field2>
          <Name>sdf</Name>
          <Id>2</Id>
       </Field2>
       <Field3>
          <Name>xyz</Name>
          <Id>3</Id>
       </Field3>
    </Response>
    

    【讨论】:

      猜你喜欢
      • 2013-07-17
      • 1970-01-01
      • 1970-01-01
      • 2015-10-29
      • 1970-01-01
      • 1970-01-01
      • 2019-07-09
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多