Java jaxb 实体打印为 xml

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2461232/
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-08-13 08:02:23  来源:igfitidea点击:

jaxb entity print out as xml

javajaxb

提问by Cristian Boariu

I have a class, let's call it Userannotated with @XmlRootElement, with some properties (name, surname etc).

我有一个类,我们称之为User注释@XmlRootElement,带有一些属性(姓名、姓氏等)。

I use this class for REST operations, as application/xml.

我将此类用于 REST 操作,如application/xml.

The client will POST User class so i want to keep the values in the log.

客户端将 POST User 类,因此我想将这些值保留在日志中。

Is there any method in jaxb to prints out this object as xml?

jaxb 中是否有任何方法可以将此对象打印为 xml

For instance:

例如:

log.info("Customers sent: "+user.whichMethod());

should produce this output:

应该产生这个输出:

Customer sent: 
<user> <name>cristi</name> <surname>kevin</surname> </user>

Thanks.

谢谢。

采纳答案by Cristian Boariu

Found:)

成立:)

public void toXml() {
    try {
        JAXBContext ctx = JAXBContext.newInstance(User.class);
        Marshaller marshaller = ctx.createMarshaller();
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
        marshaller.marshal(this, System.out);
    }
    catch (Exception
            e) {

              //catch exception 
    }
}

Call it like:

像这样称呼它:

log.info("Customers sent: "+user.toXml());

回答by java25

You can make this as a common method accessible by your endpoints.

您可以将此作为端点可访问的常用方法。

public String toXml(JAXBElement element) {
    try {
        JAXBContext jc = JAXBContext.newInstance(element.getValue().getClass());  
        Marshaller marshaller = jc.createMarshaller();  
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);  

        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        marshaller.marshal(element, baos);
        return baos.toString();
    } catch (Exception e) {
        e.printStackTrace();
    }      
    return "";
}

回答by Jin Kwon

Setting Marshaller.JAXB_FORMATTED_OUTPUTmay be not good for logging.

设置Marshaller.JAXB_FORMATTED_OUTPUT可能不利于日志记录。

Instead suppress XML Prolog (or Declaration)with Marshaller.JAXB_FRAGMENT.

而是XML Prolog (or Declaration)Marshaller.JAXB_FRAGMENT.

public static <J> String printXml(final J instance) throws JAXBException {
    return printXml(instance, instance.getClass());
}


public static <J> String printXml(final J instance,
                                  final Class<?>... classesToBeBound)
    throws JAXBException {

    final JAXBContext context = JAXBContext.newInstance(classesToBeBound);

    final ByteArrayOutputStream output = new ByteArrayOutputStream();

    final Marshaller marshaller = context.createMarshaller();
    marshaller.setProperty(Marshaller.JAXB_FRAGMENT, Boolean.TRUE);
    marshaller.marshal(instance, output);
    // output.flush(); // Nasty IOException
    final String jaxbEncoding = (String) marshaller.getProperty(
        Marshaller.JAXB_ENCODING);

    try {
        return new String(output.toByteArray(), jaxbEncoding);
    } catch (UnsupportedEncodingException uee) {
        throw new RuntimeException(uee);
    }
}

will prints a single line like this.

将打印这样的一行。

<user><name>cristi</name><surname>kevin</surname></user>

回答by Nagarjuna

public String toXml(Event event) {

    ByteArrayOutputStream baos =  null;

    try {

         JAXBContext jc = JAXBContext.newInstance(event.getClass());  
        Marshaller marshaller = jc.createMarshaller();  
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);  

        baos = new ByteArrayOutputStream();
        marshaller.marshal(event, baos);
        return baos.toString();
    } catch (JAXBException e) {
        LOGGER.log(Level.SEVERE, " problem in  Logging raw XML  :"+e.getMessage());
    }      
    return baos.toString();

This Works well

这很好用