OutputWriter.java 文件源码

java
阅读 25 收藏 0 点赞 0 评论 0

项目:LaTeXEE 作者:
/**
* Method to generated idented XML
* Credit: Steve McLeod and DaoWen.
* Code from http://stackoverflow.com/a/11519668
* @param xml input xml in string format
* @return indented xml in string format
*/
  public static String indentXML(String xml) {

      try {
          final InputSource src = new InputSource(new StringReader(xml));
          final Node document = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(src).getDocumentElement();
          final Boolean keepDeclaration = xml.startsWith("<?xml");

      //May need this: System.setProperty(DOMImplementationRegistry.PROPERTY,"com.sun.org.apache.xerces.internal.dom.DOMImplementationSourceImpl");


          final DOMImplementationRegistry registry = DOMImplementationRegistry.newInstance();
          final DOMImplementationLS impl = (DOMImplementationLS) registry.getDOMImplementation("LS");
          final LSSerializer writer = impl.createLSSerializer();

          writer.getDomConfig().setParameter("format-pretty-print", Boolean.TRUE); // Set this to true if the output needs to be beautified.
          writer.getDomConfig().setParameter("xml-declaration", keepDeclaration); // Set this to true if the declaration is needed to be outputted.

          return writer.writeToString(document);
      } catch (Exception e) {
          throw new RuntimeException(e);
      }
  }
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号