使用 JAXB 的动态 XML 属性

我有一个文件,我试图使用XMLJAXB


<root>

    <object att1="orgA" att2="orgA" id="6" name="">

        ...

    </object>

</root>

Java类:


@AllArgsConstructor

@NoArgsConstructor

@ToString

@Setter

@XmlRootElement(name="object")

public class ObjectElement implements Serializable {


@XmlAttribute

private int id;


@XmlAttribute

private String attr1;


@XmlAttribute

private String attr2;


@XmlAttribute

private String name;


}

问题:xml 元素的属性可能会动态更改。因此,属性的键可能是动态的,因此无法将它们放在类中。有没有办法定义某种读取所有键和相应值的方法?例如,新对象可能具有完全不同的属性,如下所示;objectXmlAttributesHashMapobject


<object att5="some" att7="other" id="6" name="value">

    ...

</object>


小怪兽爱吃肉
浏览 208回答 1
1回答

慕森王

您需要编写扩展类的自定义适配器。在您的情况下,我们可以创建一个表示所有属性的。读取属性后,您可以使用反射或手动设置所有字段。动态读取所有属性的自定义删除程序可能如下所示:javax.xml.bind.annotation.adapters.XmlAdapterMapPOJOclass ItemXmlAdapter extends XmlAdapter<Object, Item> {&nbsp; &nbsp; @Override&nbsp; &nbsp; public Item unmarshal(Object v) {&nbsp; &nbsp; &nbsp; &nbsp; Element element = (Element) v;&nbsp; &nbsp; &nbsp; &nbsp; Map<String, String> properties = new HashMap<>();&nbsp; &nbsp; &nbsp; &nbsp; NamedNodeMap attributes = element.getAttributes();&nbsp; &nbsp; &nbsp; &nbsp; for (int i = attributes.getLength() - 1; i >= 0; i--) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Node node = attributes.item(i);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; properties.put(node.getNodeName(), node.getNodeValue());&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; Item item = new Item();&nbsp; &nbsp; &nbsp; &nbsp; item.setProperties(properties);&nbsp; &nbsp; &nbsp; &nbsp; return item;&nbsp; &nbsp; }&nbsp; &nbsp; @Override&nbsp; &nbsp; public Object marshal(Item v) throws Exception {&nbsp; &nbsp; &nbsp; &nbsp; return null; // Implement if needed&nbsp; &nbsp; }}读取和解析所有属性的简单示例应用程序:XMLimport org.w3c.dom.Element;import org.w3c.dom.NamedNodeMap;import org.w3c.dom.Node;import javax.xml.bind.JAXBContext;import javax.xml.bind.Unmarshaller;import javax.xml.bind.annotation.XmlElement;import javax.xml.bind.annotation.XmlRootElement;import javax.xml.bind.annotation.adapters.XmlAdapter;import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;import java.io.File;import java.util.ArrayList;import java.util.HashMap;import java.util.List;import java.util.Map;public class JaxbApp {&nbsp; &nbsp; public static void main(String[] args) throws Exception {&nbsp; &nbsp; &nbsp; &nbsp; File xmlFile = new File("./resource/test.xml").getAbsoluteFile();&nbsp; &nbsp; &nbsp; &nbsp; JAXBContext context = JAXBContext.newInstance(Root.class);&nbsp; &nbsp; &nbsp; &nbsp; Unmarshaller unmarshaller = context.createUnmarshaller();&nbsp; &nbsp; &nbsp; &nbsp; Root root = (Root) unmarshaller.unmarshal(xmlFile);&nbsp; &nbsp; &nbsp; &nbsp; System.out.println(root);&nbsp; &nbsp; }}@XmlRootElement(name = "root")class Root {&nbsp; &nbsp; private List<Item> items = new ArrayList<>();&nbsp; &nbsp; @XmlElement(name = "object")&nbsp; &nbsp; public List<Item> getItems() {&nbsp; &nbsp; &nbsp; &nbsp; return items;&nbsp; &nbsp; }&nbsp; &nbsp; // getters, setters, toString}@XmlJavaTypeAdapter(ItemXmlAdapter.class)class Item {&nbsp; &nbsp; private Map<String, String> properties;&nbsp; &nbsp; // getters, setters, toString}对于以下内容:XML<root>&nbsp; &nbsp; <object att1="orgA" att2="orgA" id="6" name="N"/>&nbsp; &nbsp; <object att5="some" id="6" name="value"/></root>指纹:Items{items=[{name=N, id=6, att2=orgA, att1=orgA}, {name=value, att5=some, id=6}]}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java