我如何在java中清理XML的标头?

时间:2016-02-17 08:56:12

标签: java xml header

我希望在JAVA中清理XML的标题,如:

<?xml version="1.0" encoding="utf-8"?>
<Document>
...

DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
org.w3c.dom.Document document = builder.parse(new InputSource(new  StringReader(strXmlContent)));

我有一个带有xml

的documentBuilder
interface MyInterface1 {}
interface MyInterface2 {}

现在我不知道怎么做才能做任何帮助?

1 个答案:

答案 0 :(得分:2)

试试这个:

    Transformer transformer = TransformerFactory.newInstance().newTransformer();
    transformer.setOutputProperty( OutputKeys.INDENT, "yes" );
    StreamResult result = new StreamResult( new StringWriter() );
    DOMSource source = new DOMSource( document );
    transformer.transform( source, result );

    String xmlString = result.getWriter().toString();
    System.out.println( xmlString );

要打印回来:

{{1}}