2 回答
TA贡献1854条经验 获得超8个赞
我猜你的有效载荷如下所示:XML
<root>
<parent-element>
<id-00001>value 1</id-00001>
<id-00002>value 2</id-00002>
</parent-element>
</root>
要读取具有动态名称的节点,我们需要编写自定义适配器()。假设该模型如下所示:javax.xml.bind.annotation.adapters.XmlAdapter
@XmlRootElement(name = "root")
@XmlAccessorType(XmlAccessType.FIELD)
class Root {
@XmlElement(name = "parent-element")
@XmlJavaTypeAdapter(IdsXmlAdapter.class)
private MultiItemList list;
// getters, setters, toString
}
class MultiItemList {
private List<String> ids;
// getters, setters, toString
}
对于上述和型号,自定义适配器可能如下所示:XMLPOJO
class IdsXmlAdapter extends XmlAdapter<Object, MultiItemList> {
@Override
public MultiItemList unmarshal(Object v) {
Element element = (Element) v;
NodeList childNodes = element.getChildNodes();
List<String> ids = new ArrayList<>(childNodes.getLength());
for (int i = 0; i < childNodes.getLength(); i++) {
Node item = childNodes.item(i);
if (item.getNodeName().equals("#text")) {
continue;
}
ids.add(item.getTextContent());
}
MultiItemList multiItemList = new MultiItemList();
multiItemList.setIds(ids);
return multiItemList;
}
@Override
public Object marshal(MultiItemList v) throws Exception {
return null; // Implement if needed
}
}
用法示例:
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 {
public static void main(String[] args) throws Exception {
File xmlFile = new File("./resource/test.xml").getAbsoluteFile();
JAXBContext jaxbContext = JAXBContext.newInstance(Root.class);
Object unmarshal = jaxbContext.createUnmarshaller().unmarshal(new FileReader(xmlFile));
System.out.println(unmarshal);
}
}
指纹:
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 IllegalAnnotationExceptions
org.w3c.dom.Element is an interface, and JAXB can't handle interfaces.
this problem is related to the following location:
at org.w3c.dom.Element
TA贡献1820条经验 获得超9个赞
由于您的xml可能具有无限数量的“id元素”,因此创建匹配的POJO结构是很棘手的。您可以使用和适配器。 将不同的元素作为 dom 节点读取,您可以在适配器中处理它们。@XmlAnyElement@XmlAnyElement
例如,使用下面给出的 xml:
<parent-element>
<id-00001>value 1</id-00001>
<id-00002>value 2</id-00002>
</parent-element>
您的根可能如下所示:
@XmlRootElement(name = "parent-element")
@XmlAccessorType(XmlAccessType.FIELD)
public class Root {
@XmlAnyElement
@XmlJavaTypeAdapter(MyAdapter.class)
private List<String> varyingElements;
}
和您的适配器,如下所示:
public class MyAdapter extends XmlAdapter<Element, String> {
@Override
public Element marshal(String arg0) throws Exception {
// do what you must
return null;
}
@Override
public String unmarshal(Element element) throws Exception {
return element.getChildNodes().item(0).getNodeValue();
}
}
元素如org.w3c.dom.Element
添加回答
举报