JAXB Marshaller和输出XML的格式 [英] JAXB Marshaller and Formatting of output XML

查看:662
本文介绍了JAXB Marshaller和输出XML的格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我关注这篇文章:
JAXB Marshaller缩进

I was following this post:
JAXB Marshaller indentation

但是我遇到了一个错误:

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.

这实际上与我使用过的Marshaller有关:

Which actually pertains to the Marshaller I have used when it did:

marshaller.marshal(instance, domResult);

您的意见和建议非常受欢迎。

Your comments and opinions are highly appreciated.

干杯,

Artanis Zeratul

Cheers,
Artanis Zeratul

推荐答案

我通过调整Antonio Maria Sanchez的答案解决了我的问题位。

参考: JAXB Marshaller缩进

I fixed my problem by tweaking Antonio Maria Sanchez's answer a bit.
Reference: JAXB Marshaller indentation



所以这是我的答案:


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);


    • 而不是使用DOMResult


    • 而不是使用DOMSource


    干杯,

    Artanis Zeratul

    Cheers,
    Artanis Zeratul

    这篇关于JAXB Marshaller和输出XML的格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆