使用 Jaxb 将 XML 的一部分解组为 org.jdom.Element 的实例
Use Jaxb to unmarshall part of XML to an instance of org.jdom.Element
我正在使用严重依赖 org.jdom 项目版本 1 (http://mvnrepository.com/artifact/org.jdom/jdom/1.1.3) 和手动构建的 XML 的遗留产品,但我想尽可能多地使用 Jaxb .我们使用 Moxy 作为 Jaxb 实现。
所以说我有以下xml:
<foo bar="bar">
<baz>
<test value="something" />
</baz>
</foo>
并且由于遗留代码使用 baz 元素作为 org.jdom.Element 我想让 Jaxb 将内部元素 "baz" 解组为 org.jdom.Element 但 v 在unmarshal 始终是空字符串 (""),因此 baz 变为空。
我在下面创建了一个示例,我尝试使用 XmlAdapter 但我无法让它工作。
import java.io.ByteArrayInputStream;
import java.io.StringReader;
import java.nio.charset.StandardCharsets;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.annotation.XmlAttribute;
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 javax.xml.transform.stream.StreamSource;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.input.SAXBuilder;
import org.jdom.output.XMLOutputter;
public class Test {
public static void main(String[] args) throws JAXBException {
String fooString = "<foo bar=\"bar\"><baz><test value=\"something\" /></baz></foo>";
JAXBContext jaxbContext = org.eclipse.persistence.jaxb.JAXBContextFactory
.createContext(new Class<?>[] {Foo.class}, null);
javax.xml.bind.Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
Foo foo = unmarshaller
.unmarshal(new StreamSource(new ByteArrayInputStream(fooString.getBytes(StandardCharsets.UTF_8))), Foo.class)
.getValue();
System.out.println(foo);
}
@XmlRootElement
private static final class Foo {
@XmlAttribute
public String bar;
@XmlElement
@XmlJavaTypeAdapter(ElementAdapter.class)
public Element baz;
@Override
public String toString() {
return "Foo [bar=" + bar + ", baz=" + baz + "]";
}
}
private static final class ElementAdapter extends XmlAdapter<String, Element> {
@Override
public Element unmarshal(String v) throws Exception {
Document document = new SAXBuilder().build(new StringReader(v));
return document.getRootElement();
}
@Override
public String marshal(Element v) throws Exception {
return new XMLOutputter(org.jdom.output.Format.getPrettyFormat()).outputString(v);
}
}
}
也许我从错误的角度攻击这个。关于如何实现这一点有什么建议吗?
解组时调试问题的一个好方法是使用 javax.xml.bind.helpers.DefaultValidationEventHandler
。
在你的情况下,你可以简单地将它添加到你的 main 方法中的解组器中,例如
// ...
javax.xml.bind.Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
unmarshaller.setEventHandler(new javax.xml.bind.helpers.DefaultValidationEventHandler());
// ...
当你 运行 你的测试程序按原样附加验证处理程序时,你会看到类似下面的内容,这基本上告诉你你没有 Java class 对应于 XML 中的 test
元素。
[Exception [EclipseLink-25004] (Eclipse Persistence Services - 2.6.1.v20150916-55dc7c3): org.eclipse.persistence.exceptions.XMLMarshalException
Exception Description: An error occurred unmarshalling the document
Internal Exception: org.xml.sax.SAXParseException; lineNumber: 1; columnNumber: 47; unexpected element (uri:"", local:"test"). Expected elements are (none)]
我认为您假设您的 ElementAdapter
实现将读取并使用 baz
节点和所有子节点作为字符串。实际上,我 认为 (分页 Blaise Doughan?)发生的事情是 JAXB 正在对 XML 树进行一些预遍历,在此期间它看到您没有 test
的模型,随后丢弃 test
及其属性。
要了解这里发生的事情,首先要简化您的模型,不要担心 JDOM 元素转换。请注意,我已经更改了您的顶级 class 的名称,因此它不会与 Test
模型冲突 class:
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.transform.stream.StreamSource;
import java.io.ByteArrayInputStream;
import java.nio.charset.StandardCharsets;
public class Main {
public static void main(String[] args) throws JAXBException {
String fooString = "<foo bar=\"bar\"><baz><test value=\"something\" /></baz></foo>";
JAXBContext jaxbContext = org.eclipse.persistence.jaxb.JAXBContextFactory
.createContext(new Class<?>[] {Foo.class}, null);
javax.xml.bind.Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
unmarshaller.setEventHandler(new javax.xml.bind.helpers.DefaultValidationEventHandler());
Foo foo = unmarshaller
.unmarshal(new StreamSource(new ByteArrayInputStream(fooString.getBytes(StandardCharsets.UTF_8))), Foo.class)
.getValue();
System.out.println(foo);
}
@XmlRootElement()
public static class Foo {
@XmlAttribute
public String bar;
@XmlElement
public Baz baz;
@Override
public String toString() {
return "<foo bar=\"" + bar + "\">" + baz.toString() + "</foo>";
}
}
@XmlRootElement
public static class Baz {
@XmlElement
public Test test;
@Override
public String toString() {
return "<baz>" + test.toString() + "</baz>";
}
}
@XmlRootElement
public static class Test {
@XmlAttribute
public String value;
@Override
public String toString() {
return "<test value=\"" + value + "\"/>";
}
}
}
这应该正确解组并打印 <foo bar="bar"><baz><test value="something"/></baz></foo>
。另请注意,通常您会传递 JAXBContextFactory
一个包含整个对象图的 ObjectFactory
,这将从您的 XML 模式生成。这可能会造成混淆。
现在您可以将 XmlAdapter
添加回来,但不是尝试从 String
转换为 Element
,而是从 Baz
转换为 Element
:
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.input.SAXBuilder;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import javax.xml.bind.annotation.XmlAttribute;
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 javax.xml.transform.stream.StreamSource;
import java.io.ByteArrayInputStream;
import java.io.StringReader;
import java.io.StringWriter;
import java.nio.charset.StandardCharsets;
public class Main {
public static void main(String[] args) throws JAXBException {
String fooString = "<foo bar=\"bar\"><baz><test value=\"something\" /></baz></foo>";
JAXBContext jaxbContext = org.eclipse.persistence.jaxb.JAXBContextFactory
.createContext(new Class<?>[] {Foo.class}, null);
javax.xml.bind.Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
unmarshaller.setEventHandler(new javax.xml.bind.helpers.DefaultValidationEventHandler());
Foo foo = unmarshaller
.unmarshal(new StreamSource(new ByteArrayInputStream(fooString.getBytes(StandardCharsets.UTF_8))), Foo.class)
.getValue();
System.out.println(foo);
}
@XmlRootElement()
public static class Foo {
@XmlAttribute
public String bar;
@XmlElement
@XmlJavaTypeAdapter(ElementAdapter.class)
public Element baz;
@Override
public String toString() {
return "<foo bar=\"" + bar + "\">" + baz.toString() + "</foo>";
}
}
@XmlRootElement
public static class Baz {
@XmlElement
public Test test;
@Override
public String toString() {
return "<baz>" + test.toString() + "</baz>";
}
}
@XmlRootElement
public static class Test {
@XmlAttribute
public String value;
@Override
public String toString() {
return "<test value=\"" + value + "\"/>";
}
}
public static class ElementAdapter extends XmlAdapter<Baz, Element> {
@Override
public Element unmarshal(Baz baz) throws Exception {
StringWriter sw = new StringWriter();
// Note: it is a terrible idea to re-instantiate a context here
// Use a cached value or a singleton from before
JAXBContext jaxbContext = org.eclipse.persistence.jaxb.JAXBContextFactory
.createContext(new Class<?>[] {Baz.class}, null);
javax.xml.bind.Marshaller m = jaxbContext.createMarshaller();
m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
m.marshal(baz, sw);
Document document = new SAXBuilder().build(new StringReader(sw.toString()));
return document.getRootElement();
}
@Override
public Baz marshal(Element v) throws Exception {
// TODO implement this
return null;
}
}
}
这应该或多或少地做你想做的,尽管你可能必须在转换之前清理适配器中的一些 Baz
元素(去掉 XML 序言等)到 JDOM。请注意,在 ElementAdapter
中实例化另一个 JAXBContext
可能是一个糟糕的想法,并且可能有更有效的方法将 JAXB 元素转换为 JDOM 元素。这段代码很容易在调试器中逐步执行,因此您可以看到发生了什么。
我想我会分享一个对我有用的解决方案。
受此 post Jaxb: how to unmarshall xs:any XML-string part? 的启发,它提到 Jaxb 留下任何它不知道如何映射为 DOM 元素的东西,当标记为 @XmlAnyElement(lax = true) 我最终得到一个使用 org.w3c.dom.Element 作为中间体的 XmlAdapter class,Jaxb 知道如何映射到和映射到。
import java.io.ByteArrayInputStream;
import java.nio.charset.StandardCharsets;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.annotation.XmlAttribute;
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 javax.xml.transform.stream.StreamSource;
import org.jdom.Element;
import org.jdom.output.DOMOutputter;
import org.jdom.output.XMLOutputter;
import org.w3c.dom.Document;
public class Test {
public static void main(String[] args) throws JAXBException {
String fooString = "<foo bar=\"bar\"><baz><test value=\"something\" /></baz></foo>";
JAXBContext jaxbContext = org.eclipse.persistence.jaxb.JAXBContextFactory
.createContext(new Class<?>[] {Foo.class}, null);
javax.xml.bind.Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
Foo foo = unmarshaller
.unmarshal(new StreamSource(new ByteArrayInputStream(fooString.getBytes(StandardCharsets.UTF_8))), Foo.class)
.getValue();
System.out.println(foo);
}
@XmlRootElement
private static final class Foo {
@XmlAttribute
public String bar;
@XmlElement
@XmlJavaTypeAdapter(ElementAdapter.class)
public Element baz;
@Override
public String toString() {
return "Foo [bar=" + bar + ", baz=" + jdomElementToString(baz) + "]";
}
private String jdomElementToString(Element element) {
return new XMLOutputter(org.jdom.output.Format.getPrettyFormat()).outputString(element);
}
}
private static final class ElementAdapter extends XmlAdapter<org.w3c.dom.Element, Element> {
@Override
public Element unmarshal(org.w3c.dom.Element valueToUnmarshal) throws Exception {
org.jdom.input.DOMBuilder domBuilder = new org.jdom.input.DOMBuilder();
org.jdom.Element jdomElement = domBuilder.build(valueToUnmarshal);
return jdomElement;
}
@Override
public org.w3c.dom.Element marshal(Element elementToMarshal) throws Exception {
org.jdom.Document jdomDocument = new org.jdom.Document((Element) elementToMarshal.detach());
DOMOutputter domOutputter = new DOMOutputter();
Document domDocument = domOutputter.output(jdomDocument);
return domDocument.getDocumentElement();
}
}
}
运行 的打印结果是:
Foo [bar=bar, baz=<baz>
<test value="something" />
</baz>]
如您所见,baz 标记中的内容已分配给 "as-is" Foo class 中的变量。
我正在使用严重依赖 org.jdom 项目版本 1 (http://mvnrepository.com/artifact/org.jdom/jdom/1.1.3) 和手动构建的 XML 的遗留产品,但我想尽可能多地使用 Jaxb .我们使用 Moxy 作为 Jaxb 实现。
所以说我有以下xml:
<foo bar="bar">
<baz>
<test value="something" />
</baz>
</foo>
并且由于遗留代码使用 baz 元素作为 org.jdom.Element 我想让 Jaxb 将内部元素 "baz" 解组为 org.jdom.Element 但 v 在unmarshal 始终是空字符串 (""),因此 baz 变为空。 我在下面创建了一个示例,我尝试使用 XmlAdapter 但我无法让它工作。
import java.io.ByteArrayInputStream;
import java.io.StringReader;
import java.nio.charset.StandardCharsets;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.annotation.XmlAttribute;
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 javax.xml.transform.stream.StreamSource;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.input.SAXBuilder;
import org.jdom.output.XMLOutputter;
public class Test {
public static void main(String[] args) throws JAXBException {
String fooString = "<foo bar=\"bar\"><baz><test value=\"something\" /></baz></foo>";
JAXBContext jaxbContext = org.eclipse.persistence.jaxb.JAXBContextFactory
.createContext(new Class<?>[] {Foo.class}, null);
javax.xml.bind.Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
Foo foo = unmarshaller
.unmarshal(new StreamSource(new ByteArrayInputStream(fooString.getBytes(StandardCharsets.UTF_8))), Foo.class)
.getValue();
System.out.println(foo);
}
@XmlRootElement
private static final class Foo {
@XmlAttribute
public String bar;
@XmlElement
@XmlJavaTypeAdapter(ElementAdapter.class)
public Element baz;
@Override
public String toString() {
return "Foo [bar=" + bar + ", baz=" + baz + "]";
}
}
private static final class ElementAdapter extends XmlAdapter<String, Element> {
@Override
public Element unmarshal(String v) throws Exception {
Document document = new SAXBuilder().build(new StringReader(v));
return document.getRootElement();
}
@Override
public String marshal(Element v) throws Exception {
return new XMLOutputter(org.jdom.output.Format.getPrettyFormat()).outputString(v);
}
}
}
也许我从错误的角度攻击这个。关于如何实现这一点有什么建议吗?
解组时调试问题的一个好方法是使用 javax.xml.bind.helpers.DefaultValidationEventHandler
。
在你的情况下,你可以简单地将它添加到你的 main 方法中的解组器中,例如
// ...
javax.xml.bind.Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
unmarshaller.setEventHandler(new javax.xml.bind.helpers.DefaultValidationEventHandler());
// ...
当你 运行 你的测试程序按原样附加验证处理程序时,你会看到类似下面的内容,这基本上告诉你你没有 Java class 对应于 XML 中的 test
元素。
[Exception [EclipseLink-25004] (Eclipse Persistence Services - 2.6.1.v20150916-55dc7c3): org.eclipse.persistence.exceptions.XMLMarshalException
Exception Description: An error occurred unmarshalling the document
Internal Exception: org.xml.sax.SAXParseException; lineNumber: 1; columnNumber: 47; unexpected element (uri:"", local:"test"). Expected elements are (none)]
我认为您假设您的 ElementAdapter
实现将读取并使用 baz
节点和所有子节点作为字符串。实际上,我 认为 (分页 Blaise Doughan?)发生的事情是 JAXB 正在对 XML 树进行一些预遍历,在此期间它看到您没有 test
的模型,随后丢弃 test
及其属性。
要了解这里发生的事情,首先要简化您的模型,不要担心 JDOM 元素转换。请注意,我已经更改了您的顶级 class 的名称,因此它不会与 Test
模型冲突 class:
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.transform.stream.StreamSource;
import java.io.ByteArrayInputStream;
import java.nio.charset.StandardCharsets;
public class Main {
public static void main(String[] args) throws JAXBException {
String fooString = "<foo bar=\"bar\"><baz><test value=\"something\" /></baz></foo>";
JAXBContext jaxbContext = org.eclipse.persistence.jaxb.JAXBContextFactory
.createContext(new Class<?>[] {Foo.class}, null);
javax.xml.bind.Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
unmarshaller.setEventHandler(new javax.xml.bind.helpers.DefaultValidationEventHandler());
Foo foo = unmarshaller
.unmarshal(new StreamSource(new ByteArrayInputStream(fooString.getBytes(StandardCharsets.UTF_8))), Foo.class)
.getValue();
System.out.println(foo);
}
@XmlRootElement()
public static class Foo {
@XmlAttribute
public String bar;
@XmlElement
public Baz baz;
@Override
public String toString() {
return "<foo bar=\"" + bar + "\">" + baz.toString() + "</foo>";
}
}
@XmlRootElement
public static class Baz {
@XmlElement
public Test test;
@Override
public String toString() {
return "<baz>" + test.toString() + "</baz>";
}
}
@XmlRootElement
public static class Test {
@XmlAttribute
public String value;
@Override
public String toString() {
return "<test value=\"" + value + "\"/>";
}
}
}
这应该正确解组并打印 <foo bar="bar"><baz><test value="something"/></baz></foo>
。另请注意,通常您会传递 JAXBContextFactory
一个包含整个对象图的 ObjectFactory
,这将从您的 XML 模式生成。这可能会造成混淆。
现在您可以将 XmlAdapter
添加回来,但不是尝试从 String
转换为 Element
,而是从 Baz
转换为 Element
:
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.input.SAXBuilder;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import javax.xml.bind.annotation.XmlAttribute;
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 javax.xml.transform.stream.StreamSource;
import java.io.ByteArrayInputStream;
import java.io.StringReader;
import java.io.StringWriter;
import java.nio.charset.StandardCharsets;
public class Main {
public static void main(String[] args) throws JAXBException {
String fooString = "<foo bar=\"bar\"><baz><test value=\"something\" /></baz></foo>";
JAXBContext jaxbContext = org.eclipse.persistence.jaxb.JAXBContextFactory
.createContext(new Class<?>[] {Foo.class}, null);
javax.xml.bind.Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
unmarshaller.setEventHandler(new javax.xml.bind.helpers.DefaultValidationEventHandler());
Foo foo = unmarshaller
.unmarshal(new StreamSource(new ByteArrayInputStream(fooString.getBytes(StandardCharsets.UTF_8))), Foo.class)
.getValue();
System.out.println(foo);
}
@XmlRootElement()
public static class Foo {
@XmlAttribute
public String bar;
@XmlElement
@XmlJavaTypeAdapter(ElementAdapter.class)
public Element baz;
@Override
public String toString() {
return "<foo bar=\"" + bar + "\">" + baz.toString() + "</foo>";
}
}
@XmlRootElement
public static class Baz {
@XmlElement
public Test test;
@Override
public String toString() {
return "<baz>" + test.toString() + "</baz>";
}
}
@XmlRootElement
public static class Test {
@XmlAttribute
public String value;
@Override
public String toString() {
return "<test value=\"" + value + "\"/>";
}
}
public static class ElementAdapter extends XmlAdapter<Baz, Element> {
@Override
public Element unmarshal(Baz baz) throws Exception {
StringWriter sw = new StringWriter();
// Note: it is a terrible idea to re-instantiate a context here
// Use a cached value or a singleton from before
JAXBContext jaxbContext = org.eclipse.persistence.jaxb.JAXBContextFactory
.createContext(new Class<?>[] {Baz.class}, null);
javax.xml.bind.Marshaller m = jaxbContext.createMarshaller();
m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
m.marshal(baz, sw);
Document document = new SAXBuilder().build(new StringReader(sw.toString()));
return document.getRootElement();
}
@Override
public Baz marshal(Element v) throws Exception {
// TODO implement this
return null;
}
}
}
这应该或多或少地做你想做的,尽管你可能必须在转换之前清理适配器中的一些 Baz
元素(去掉 XML 序言等)到 JDOM。请注意,在 ElementAdapter
中实例化另一个 JAXBContext
可能是一个糟糕的想法,并且可能有更有效的方法将 JAXB 元素转换为 JDOM 元素。这段代码很容易在调试器中逐步执行,因此您可以看到发生了什么。
我想我会分享一个对我有用的解决方案。
受此 post Jaxb: how to unmarshall xs:any XML-string part? 的启发,它提到 Jaxb 留下任何它不知道如何映射为 DOM 元素的东西,当标记为 @XmlAnyElement(lax = true) 我最终得到一个使用 org.w3c.dom.Element 作为中间体的 XmlAdapter class,Jaxb 知道如何映射到和映射到。
import java.io.ByteArrayInputStream;
import java.nio.charset.StandardCharsets;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.annotation.XmlAttribute;
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 javax.xml.transform.stream.StreamSource;
import org.jdom.Element;
import org.jdom.output.DOMOutputter;
import org.jdom.output.XMLOutputter;
import org.w3c.dom.Document;
public class Test {
public static void main(String[] args) throws JAXBException {
String fooString = "<foo bar=\"bar\"><baz><test value=\"something\" /></baz></foo>";
JAXBContext jaxbContext = org.eclipse.persistence.jaxb.JAXBContextFactory
.createContext(new Class<?>[] {Foo.class}, null);
javax.xml.bind.Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
Foo foo = unmarshaller
.unmarshal(new StreamSource(new ByteArrayInputStream(fooString.getBytes(StandardCharsets.UTF_8))), Foo.class)
.getValue();
System.out.println(foo);
}
@XmlRootElement
private static final class Foo {
@XmlAttribute
public String bar;
@XmlElement
@XmlJavaTypeAdapter(ElementAdapter.class)
public Element baz;
@Override
public String toString() {
return "Foo [bar=" + bar + ", baz=" + jdomElementToString(baz) + "]";
}
private String jdomElementToString(Element element) {
return new XMLOutputter(org.jdom.output.Format.getPrettyFormat()).outputString(element);
}
}
private static final class ElementAdapter extends XmlAdapter<org.w3c.dom.Element, Element> {
@Override
public Element unmarshal(org.w3c.dom.Element valueToUnmarshal) throws Exception {
org.jdom.input.DOMBuilder domBuilder = new org.jdom.input.DOMBuilder();
org.jdom.Element jdomElement = domBuilder.build(valueToUnmarshal);
return jdomElement;
}
@Override
public org.w3c.dom.Element marshal(Element elementToMarshal) throws Exception {
org.jdom.Document jdomDocument = new org.jdom.Document((Element) elementToMarshal.detach());
DOMOutputter domOutputter = new DOMOutputter();
Document domDocument = domOutputter.output(jdomDocument);
return domDocument.getDocumentElement();
}
}
}
运行 的打印结果是:
Foo [bar=bar, baz=<baz>
<test value="something" />
</baz>]
如您所见,baz 标记中的内容已分配给 "as-is" Foo class 中的变量。