java 如何获得 XmlType 的字符串表示?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6032066/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 14:03:39  来源:igfitidea点击:

How to get String representation of XmlType?

javaxmljaxb

提问by ryan

Is it possible to convert a javax.xml.bind.annotation.XmlType to the String representation of the XML?

是否可以将 javax.xml.bind.annotation.XmlType 转换为 XML 的字符串表示形式?

Example:

例子:

The following class Req is from a third party library so I can't override the toString() method.

以下类 Req 来自第三方库,因此我无法覆盖 toString() 方法。

@javax.xml.bind.annotation.XmlAccessorType(javax.xml.bind.annotation.XmlAccessType.FIELD)
@javax.xml.bind.annotation.XmlType(name = "req", propOrder = {"myDetails", "customerDetails"})
public class Req  {
...
}

In my application I want to simply get a String representation of the XML so that I can log it to a file:

在我的应用程序中,我只想获取 XML 的字符串表示,以便我可以将其记录到文件中:

<Req>
    <MyDetails>
    ...
    </MyDetails>
    <CustomerDetails>
    ...
    </CustomerDetails>
</Req>

When I try to use JAXB and Marshall to convert to XML String:

当我尝试使用 JAXB 和 Marshall 转换为 XML 字符串时:

JAXBContext context = JAXBContext.newInstance(Req.class);
Marshaller marshaller = context.createMarshaller();
StringWriter sw = new StringWriter();
marshaller.marshal(instanceOfReq, sw);
String xmlString = sw.toString();

I get the following exception:

我收到以下异常:

javax.xml.bind.MarshalException
    - with linked exception:
    [com.sun.istack.SAXException2: unable to marshal type "mypackage.Req" as an element because it is missing an @XmlRootElement annotation]

I have had a look at the other classes within the third party library and none of them use the @XmlRootElement annotation. Any way around this?

我查看了第三方库中的其他类,但没有一个使用 @XmlRootElement 注释。有什么办法解决这个问题吗?

回答by Bala R

You can use JAXB and marshall it to xml string

您可以使用 JAXB 并将其编组为 xml 字符串

JAXBContext context = JAXBContext.newInstance(Req.class);
Marshaller marshaller = context.createMarshaller();
StringWriter sw = new StringWriter();
marshaller.marshal(instanceOfReq, sw);

String xmlString = sw.toString();

回答by Christian Vielma

Addding to what Bala R indicated, you can do this if your JAXB element don't have the @xmlrootelement

添加到 Bala R 指示的内容,如果您的 JAXB 元素没有 @xmlrootelement

JAXBContext context = JAXBContext.newInstance(YourClass.class);
            Marshaller marshaller = context.createMarshaller();
            marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
            StringWriter sw = new StringWriter();
            JAXBElement jx = new JAXBElement(new QName("YourRootElement"), YourClass.class, input);
            marshaller.marshal(jx, sw);
            String xmlString = sw.toString();

This was also stated here.

此处也说明了这一点