remove xml declaration from the generated xml document using java

前端 未结 4 1162
面向向阳花
面向向阳花 2020-12-07 01:03
String root = \"RdbTunnels\";
DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder documentBuilder = documentBuilder         


        
相关标签:
4条回答
  • 2020-12-07 01:50

    By using the setOmitXMLDeclaration(true); method from the Format class. I'm not sure but I think it uses jDom lib.

    Example (it will display the XML file without the XML declaration of the Document document)

    XMLOutputter out= new XMLOutputter(Format.getCompactFormat().setOmitDeclaration(true));
    out.output(document, System.out);
    
    0 讨论(0)
  • 2020-12-07 01:55

    Have you seen OutputKeys as used by Transformer? Specifically OMIT_XML_DECLARATION.

    Note that removing the header is valid in XML 1.0, but you lose character encoding data (among other things) which can be very important.

    0 讨论(0)
  • 2020-12-07 02:00

    Add this

    format.setOmitXMLDeclaration(true);
    

    Example

    OutputFormat format = new OutputFormat(document);
    format.setIndenting(true);
    format.setOmitXMLDeclaration(true);
    
    0 讨论(0)
  • 2020-12-07 02:02

    This code avoid you xml first line of xml declaration for this use i used xerces-1.4.4.jar

    import org.w3c.dom.Document;
    import org.xml.sax.InputSource;
    import org.xml.sax.SAXException;
    
    import javax.xml.parsers.DocumentBuilder;
    import javax.xml.parsers.DocumentBuilderFactory;
    import javax.xml.parsers.ParserConfigurationException;
    import java.io.IOException;
    import java.io.StringReader;
    import java.io.StringWriter;
    import java.io.Writer;
    
    import org.apache.xml.serialize.OutputFormat;
    import org.apache.xml.serialize.XMLSerializer;
    
    
    public class TextToXmlFormatter {
    
        public TextToXmlFormatter() {
        }
    
        public String format(String unformattedXml) {
            try {
                final Document document = parseXmlFile(unformattedXml);
    
                OutputFormat format = new OutputFormat(document);
                format.setLineWidth(65);
                format.setIndenting(true);
                format.setIndent(2);
                format.setOmitXMLDeclaration(true);
                Writer out = new StringWriter();
                XMLSerializer serializer = new XMLSerializer(out, format);
                serializer.serialize(document);
    
                return out.toString();
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
    
        private Document parseXmlFile(String in) {
            try {
                DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
                DocumentBuilder db = dbf.newDocumentBuilder();
                InputSource is = new InputSource(new StringReader(in));
                return db.parse(is);
            } catch (ParserConfigurationException e) {
                throw new RuntimeException(e);
            } catch (SAXException e) {
                throw new RuntimeException(e);
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
    
        public static void main(String[] args) {
            String unformattedXml =
                    "YOUR XML STRING";
    
            System.out.println(new TextToXmlFormatter().format(unformattedXml));
        }
    
    }
    
    0 讨论(0)
提交回复
热议问题