How to I output org.w3c.dom.Element to string format in java?

前端 未结 7 647
旧巷少年郎
旧巷少年郎 2020-11-27 13:01

I have an org.w3c.dom.Element object passed into my method. I need to see the whole xml string including its child nodes (the whole object graph). I am looking

相关标签:
7条回答
  • 2020-11-27 14:02

    Simple 4 lines code to get String without xml-declaration (<?xml version="1.0" encoding="UTF-16"?>) from org.w3c.dom.Element

    DOMImplementationLS lsImpl = (DOMImplementationLS)node.getOwnerDocument().getImplementation().getFeature("LS", "3.0");
    LSSerializer serializer = lsImpl.createLSSerializer();
    serializer.getDomConfig().setParameter("xml-declaration", false); //by default its true, so set it to false to get String without xml-declaration
    String str = serializer.writeToString(node);
    
    0 讨论(0)
提交回复
热议问题