[英]JAXB append a xml into a object to be marshaled
我想將封送處理的對象(xml代碼)附加到另一個要封送處理的對象中。
@XmlRootElement
object Child{
...
}
@XmlRootElement
object Parent{
@XmlElement
object Any;
}
封送兒童:
<child xmlns="namespaceOfChild.org">
<...>
<...>
<\child>
我想在Parent.Any上設置上面的xml,以在將Parent封送后導致代碼下面的代碼。
<parent xmlns="namespaceOfParent.org">
<any>
<child xmlns="namespaceOfChild.org">
<...>
<...>
<\child>
<\any>
<\parent>
請注意,名稱空間和child的其他屬性必須遵循child tag
如上面的代碼所示。 當我將對象Child本身設置在Parent.Any上時,我獲得了成功,但是Child的屬性看起來像Parent的屬性。
我知道了! 我可以將內部元素Child
保存在Document
(org.w3c.dom.Document)中,並使用該函數將返回document.getDocumentElement()
的Child
編組到Node
(org.w3c.dom.Node)。 然后,我使用此Node
在Parent.Any
設置對象,然后將其編組為Parent
。
封送兒童的功能:
private static Node marshal(Object obj) throws JAXBException {
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
dbf.setNamespaceAware(true);
Document doc = null;
try {
DocumentBuilder db = dbf.newDocumentBuilder();
doc = db.newDocument();
} catch (ParserConfigurationException ex) {
throw new JAXBException(ex);
}
context = JAXBContext.newInstance("org.openarchives.oai._2_0.oai_dc");
Marshaller m = context.createMarshaller();
m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
m.marshal(obj, doc);
return doc.getDocumentElement();
}
封送家長的功能:
private static void marshal(Object obj, OutputStream stream) throws JAXBException {
context = JAXBContext.newInstance("org.openarchives.oai._2");
Marshaller m = context.createMarshaller();
m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
m.marshal(obj, stream);
}
當我編組Parent時,Child的屬性在<child>
內,而Parent的屬性在<parent>
。
很好的答案,它指導了我最終解決方案。
對於你們中的一個,他們正在嘗試使用JAX-RS和JERSEY JAXB實現,這是我要做的最后步驟。
假設我們有一個名為metadataType的父類,其子類的XML類型為“ any”,我們想分配一個名為Record的類。
這是我創建對象時所做的對象的“設置”
Document doc = null;
try {
JAXBContext jaxbContext = JAXBContext.newInstance(OAIPMHtype.class,Record.class,RDF.class);
Marshaller jaxbMarshaller = jaxbContext.createMarshaller();
jaxbMarshaller.setProperty(Marshaller.JAXB_SCHEMA_LOCATION, "http://www.europeana.eu/schemas/ese/ " +
"http://www.europeana.eu/schemas/ese/ESE-V3.4.xsd");
jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
dbf.setNamespaceAware(true);
try {
DocumentBuilder db = dbf.newDocumentBuilder();
doc = db.newDocument();
} catch (ParserConfigurationException ex) {
throw new JAXBException(ex);
}
jaxbMarshaller.marshal(record, doc);
} catch (JAXBException e) {
throw new JAXBException(e);
}
metadataType.setAny(doc.getDocumentElement());
在Record類中,我必須添加@XMLRootElement,例如:
@XmlRootElement(name = "record",namespace = "http://www.europeana.eu/schemas/ese/")
然后,如果您自動讓澤西·馬歇爾(Jersey Marshall)自動生成該對象
<root xmlns="http://rootschema"><metadata><ese:record xmlns:ese="http://www.europeana.eu/schemas/ese/" schemaLocation="http://www.europeana.eu/schemas/ese/ http://www.europeana.eu/schemas/ese/ESE-V3.4.xsd"> <metadata> </root>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.