JAXB/XSD:数字而不是元素名称

我必须从xml文件创建Java对象(转换前我无法编辑),所以我认为最好创建一个XSD,然后通过JAXB取消marshall文件。问题是,该 XML 中有一些元素的格式如下所示:

<parent-element>
    <id-00001>...</id-00001>
    <id-00002>...</id-00002>
    ...
</parent-element>

所以基本上我有一个xml元素列表,元素的“名称”是列表中的顺序。每个列表中的元素数量可能不受限制。是否可以使用 JAXB 对此进行解构,或者我是否必须使用 XML 读取器并循环访问这些元素?“id-XXXXX”元素在内部都以相同的方式格式化,因此,如果将其格式化为“”,则可以没有任何问题。

提前致谢!


动漫人物
浏览 95回答 2
2回答

哔哔one

我猜你的有效载荷如下所示:XML<root>&nbsp; &nbsp; <parent-element>&nbsp; &nbsp; &nbsp; &nbsp; <id-00001>value 1</id-00001>&nbsp; &nbsp; &nbsp; &nbsp; <id-00002>value 2</id-00002>&nbsp; &nbsp; </parent-element></root>要读取具有动态名称的节点,我们需要编写自定义适配器()。假设该模型如下所示:javax.xml.bind.annotation.adapters.XmlAdapter@XmlRootElement(name = "root")@XmlAccessorType(XmlAccessType.FIELD)class Root {&nbsp; &nbsp; @XmlElement(name = "parent-element")&nbsp; &nbsp; @XmlJavaTypeAdapter(IdsXmlAdapter.class)&nbsp; &nbsp; private MultiItemList list;&nbsp; &nbsp; // getters, setters, toString}class MultiItemList {&nbsp; &nbsp; private List<String> ids;&nbsp; &nbsp; // getters, setters, toString}对于上述和型号,自定义适配器可能如下所示:XMLPOJOclass IdsXmlAdapter extends XmlAdapter<Object, MultiItemList> {&nbsp; &nbsp; @Override&nbsp; &nbsp; public MultiItemList unmarshal(Object v) {&nbsp; &nbsp; &nbsp; &nbsp; Element element = (Element) v;&nbsp; &nbsp; &nbsp; &nbsp; NodeList childNodes = element.getChildNodes();&nbsp; &nbsp; &nbsp; &nbsp; List<String> ids = new ArrayList<>(childNodes.getLength());&nbsp; &nbsp; &nbsp; &nbsp; for (int i = 0; i < childNodes.getLength(); i++) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Node item = childNodes.item(i);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (item.getNodeName().equals("#text")) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; continue;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ids.add(item.getTextContent());&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; MultiItemList multiItemList = new MultiItemList();&nbsp; &nbsp; &nbsp; &nbsp; multiItemList.setIds(ids);&nbsp; &nbsp; &nbsp; &nbsp; return multiItemList;&nbsp; &nbsp; }&nbsp; &nbsp; @Override&nbsp; &nbsp; public Object marshal(MultiItemList v) throws Exception {&nbsp; &nbsp; &nbsp; &nbsp; return null; // Implement if needed&nbsp; &nbsp; }}用法示例:import org.w3c.dom.Element;import org.w3c.dom.Node;import org.w3c.dom.NodeList;import javax.xml.bind.JAXBContext;import javax.xml.bind.annotation.XmlAccessType;import javax.xml.bind.annotation.XmlAccessorType;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.io.FileReader;import java.util.ArrayList;import java.util.List;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 jaxbContext = JAXBContext.newInstance(Root.class);&nbsp; &nbsp; &nbsp; &nbsp; Object unmarshal = jaxbContext.createUnmarshaller().unmarshal(new FileReader(xmlFile));&nbsp; &nbsp; &nbsp; &nbsp; System.out.println(unmarshal);&nbsp; &nbsp; }}指纹:Root{list=Root{ids=[value 1, value 2]}}请注意,由于异常,您无法直接实现适配器:XmlAdapter<Element, MultiItemList>Exception in thread "main" com.sun.xml.internal.bind.v2.runtime.IllegalAnnotationsException: 1 counts of IllegalAnnotationExceptionsorg.w3c.dom.Element is an interface, and JAXB can't handle interfaces.&nbsp; &nbsp; this problem is related to the following location:&nbsp; &nbsp; &nbsp; &nbsp; at org.w3c.dom.Element

慕妹3146593

由于您的xml可能具有无限数量的“id元素”,因此创建匹配的POJO结构是很棘手的。您可以使用和适配器。 将不同的元素作为 dom 节点读取,您可以在适配器中处理它们。@XmlAnyElement@XmlAnyElement例如,使用下面给出的 xml:<parent-element>&nbsp; &nbsp; <id-00001>value 1</id-00001>&nbsp; &nbsp; <id-00002>value 2</id-00002></parent-element>您的根可能如下所示:@XmlRootElement(name = "parent-element")@XmlAccessorType(XmlAccessType.FIELD)public class Root {&nbsp; &nbsp; @XmlAnyElement&nbsp; &nbsp; @XmlJavaTypeAdapter(MyAdapter.class)&nbsp; &nbsp; private List<String> varyingElements;}和您的适配器,如下所示:public class MyAdapter extends XmlAdapter<Element, String> {&nbsp; &nbsp; @Override&nbsp; &nbsp; public Element marshal(String arg0) throws Exception {&nbsp; &nbsp; &nbsp; &nbsp; // do what you must&nbsp; &nbsp; &nbsp; &nbsp; return null;&nbsp; &nbsp; }&nbsp; &nbsp; @Override&nbsp; &nbsp; public String unmarshal(Element element) throws Exception {&nbsp; &nbsp; &nbsp; &nbsp; return element.getChildNodes().item(0).getNodeValue();&nbsp; &nbsp; }}元素如org.w3c.dom.Element
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java