Jaxb Unmarshalling(具有相同元素的不同包装元素)
Jaxb Unmarshalling (different wrapper elements with same element)
我有以下 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 结构。我找不到解决方案。任何指针都会有所帮助。
你可以对你做这样的事情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;
}
JAXB 会在遇到不同的 FIelds
.
时自动映射到字段 class
EDITED/UPDATED
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>
我有以下 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 结构。我找不到解决方案。任何指针都会有所帮助。
你可以对你做这样的事情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;
}
JAXB 会在遇到不同的 FIelds
.
EDITED/UPDATED
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>