简体   繁体   English

JAXB编组器和输出XML的格式

[英]JAXB Marshaller and Formatting of output XML

I was following this post: 我正在关注这篇文章:
JAXB Marshaller indentation JAXB马歇尔缩进

But I ran to an error: 但是我遇到了一个错误:

org.w3c.dom.DOMException: NAMESPACE_ERR: An attempt is made to create or change an object in a way which is incorrect with regard to namespaces.

Which actually pertains to the Marshaller I have used when it did: 实际上与我使用过的Marshaller有关:

marshaller.marshal(instance, domResult);

Your comments and opinions are highly appreciated. 非常感谢您的意见和建议。

Cheers, 干杯,
Artanis Zeratul 阿塔尼斯·泽拉图尔(Artanis Zeratul)

I fixed my problem by tweaking Antonio Maria Sanchez's answer a bit. 我通过调整安东尼奥·玛丽亚·桑切斯的答案来解决我的问题。
Reference: JAXB Marshaller indentation 参考: JAXB马歇尔缩进


So here is my answer: 所以这是我的答案:

import java.io.File;
import java.io.FileNotFoundException;
import java.io.StringReader;
import java.io.StringWriter;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamSource;

public class ObjectToXMLWriter {
    public static <Type> boolean writeToFileWithXmlTransformer(Type instance
            ,String fullFileNamePath) throws FileNotFoundException {
        boolean isSaved = false;
        JAXBContext jaxBContent = null;
        Marshaller marshaller = null;
        StringWriter stringWriter = new StringWriter();

        try {
            jaxBContent = JAXBContext.newInstance(instance.getClass());
            marshaller = jaxBContent.createMarshaller();
            marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
            marshaller.marshal(instance, stringWriter);

            Transformer transformer = TransformerFactory.newInstance().newTransformer();
            transformer.setOutputProperty(OutputKeys.INDENT, "yes");
            transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");

            transformer.transform(new StreamSource(new StringReader(stringWriter.toString()))
                    ,new StreamResult(new File(fullFileNamePath)));

           isSaved = true; 
        } catch(JAXBException jaxBException) {
            System.out.println("JAXBException happened!");
            jaxBException.printStackTrace();
        } catch(Exception exception) {
            System.out.println("Exception happened!");
            exception.printStackTrace();
        }

        return isSaved;
    }
}


The critical points to this answer are the following: 该答案的关键点如下:

  • marshaller.marshal(instance, stringWriter); marshaller.marshal(instance,stringWriter);
    • instead of using DOMResult 而不是使用DOMResult
  • transformer.transform(new StreamSource(new StringReader(stringWriter.toString())) ,new StreamResult(new File(fullFileNamePath))); Transformer.transform(new StreamSource(new StringReader(stringWriter.toString()),new StreamResult(new File(fullFileNamePath))));
    • instead of using DOMSource 而不是使用DOMSource


Cheers, 干杯,
Artanis Zeratul 阿塔尼斯·泽拉图尔(Artanis Zeratul)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM