Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/375.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java JAXB封送器和输出XML的格式化_Java_Jaxb_Marshalling - Fatal编程技术网

Java JAXB封送器和输出XML的格式化

Java JAXB封送器和输出XML的格式化,java,jaxb,marshalling,Java,Jaxb,Marshalling,我在看这篇文章: 但我遇到了一个错误: 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有关: marshaller.marshal(instance, domResult); 非常感谢您的评论和意见 干杯, 阿塔

我在看这篇文章:

但我遇到了一个错误:

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有关:

marshaller.marshal(instance, domResult);
非常感谢您的评论和意见

干杯,

阿塔尼斯·泽拉图尔(Artanis Zeratul)

我通过稍微调整安东尼奥·玛丽亚·桑切斯(Antonio Maria Sanchez)的答案,解决了我的问题。
参考:


我的回答是:

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;
    }
}
导入java.io.File;
导入java.io.FileNotFoundException;
导入java.io.StringReader;
导入java.io.StringWriter;
导入javax.xml.bind.JAXBContext;
导入javax.xml.bind.JAXBException;
导入javax.xml.bind.Marshaller;
导入javax.xml.transform.OutputKeys;
导入javax.xml.transform.Transformer;
导入javax.xml.transform.TransformerFactory;
导入javax.xml.transform.stream.StreamResult;
导入javax.xml.transform.stream.StreamSource;
公共类ObjectOxMLWriter{
公共静态布尔writeToFileWithXmlTransformer(类型实例
,字符串fullFileNamePath)引发FileNotFoundException{
布尔值isSaved=false;
JAXBContext jaxBContent=null;
Marshaller=null;
StringWriter StringWriter=新StringWriter();
试一试{
jaxBContent=JAXBContext.newInstance(instance.getClass());
marshaller=jaxBContent.createMarshaller();
setProperty(marshaller.JAXB_格式化的_输出,true);
marshaller.marshall(实例,stringWriter);
Transformer Transformer=TransformerFactory.newInstance().newTransformer();
transformer.setOutputProperty(OutputKeys.INDENT,“是”);
transformer.setOutputProperty(“{http://xml.apache.org/xslt}缩进金额,“2”);
transformer.transform(新的StreamSource(新的StringReader(stringWriter.toString()))
,新的StreamResult(新文件(fullFileNamePath));
isSaved=真;
}捕获(JAXBEException JAXBEException){
System.out.println(“发生了JAXBEException!”);
jaxBException.printStackTrace();
}捕获(异常){
System.out.println(“发生异常!”);
异常。printStackTrace();
}
保留了回报;
}
}

这个答案的关键点如下:

  • marshaller.marshall(实例,stringWriter);
    • 而不是使用DOMResult
  • transformer.transform(新的StreamSource(新的StringReader(stringWriter.toString())) ,新的StreamResult(新文件(fullFileNamePath));
    • 而不是使用DOMSource

干杯,
阿塔尼斯·泽拉图尔