Java XML Output - proper indenting for child items

后端 未结 3 1979
梦谈多话
梦谈多话 2021-02-11 05:50

I\'d like to serialize some simple data model into xml, I\'ve been using the standard java.org.w3c -related code (see below), the indentation is better than no \"OutputKeys.INDE

相关标签:
3条回答
  • 2021-02-11 05:57

    If the Transformer implementation you're using is Xalan-J, then you should be able to use:

    transformer.setOutputProperty(
       "{http://xml.apache.org/xslt}indent-amount", "5");
    

    See also: http://xml.apache.org/xalan-j/usagepatterns.html

    0 讨论(0)
  • 2021-02-11 05:59
    import com.sun.org.apache.xml.internal.serializer.OutputPropertiesFactory
    
    transformer.setOutputProperty(OutputPropertiesFactory.S_KEY_INDENT_AMOUNT, "4");
    
    0 讨论(0)
  • 2021-02-11 06:01
    Document doc;
    
    .....
    
    TransformerFactory factory = TransformerFactory.newInstance();
    Transformer transformer = factory.newTransformer();
    transformer.setOutputProperty(OutputKeys.INDENT, "yes");
    transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4");
    transformer.transform(new DOMSource(doc), new StreamResult(new File("filename.xml")));
    transformer.transform(new DOMSource(doc), new StreamResult(System.out));
    
    0 讨论(0)
提交回复
热议问题