【问题标题】:JAXB Marshaller and Formatting of output XMLJAXB Marshaller 和输出 XML 的格式
【发布时间】:2018-11-08 22:34:03
【问题描述】:

我在关注这个帖子:
JAXB Marshaller indentation

但是我遇到了一个错误:

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

非常感谢您的 cmets 和意见。

干杯,
阿塔尼斯·泽拉图

【问题讨论】:

    标签: java jaxb marshalling


    【解决方案1】:

    我通过稍微调整 Antonio Maria Sanchez 的答案解决了我的问题。
    参考:JAXB Marshaller indentation


    所以这是我的答案:

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


    这个答案的关键点如下:

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


    干杯,
    阿塔尼斯·泽拉图

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-12-06
      • 2018-03-24
      • 1970-01-01
      • 1970-01-01
      • 2015-06-14
      • 1970-01-01
      • 2013-03-20
      相关资源
      最近更新 更多