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
添加回答
舉報