有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

使用java从生成的xml文档中删除xml声明

String root = "RdbTunnels";
DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
Document document = documentBuilder.newDocument();
Element rootElement = document.createElement(root);
document.appendChild(rootElement);   

OutputFormat format = new OutputFormat(document);
format.setIndenting(true);


XMLSerializer serializer = new XMLSerializer(System.out, format);
serializer.serialize(document);

给出如下结果

<?xml version="1.0" encoding="UTF-8"?>
<RdbTunnels/>

但是我需要从输出中删除xml声明,我该怎么做呢


共 (4) 个答案

  1. # 1 楼答案

    通过使用setOmitXMLDeclaration(true);方法从Format类中删除。 我不确定,但我认为它使用jDom库

    示例(它将显示XML文件,而不显示文档的XML声明)

    XMLOutputter out= new XMLOutputter(Format.getCompactFormat().setOmitDeclaration(true));
    out.output(document, System.out);
    
  2. # 2 楼答案

    加上这个

    format.setOmitXMLDeclaration(true);
    

    范例

    OutputFormat format = new OutputFormat(document);
    format.setIndenting(true);
    format.setOmitXMLDeclaration(true);
    
  3. # 3 楼答案

    你见过OutputKeysTransformer使用的OutputKeys吗?特别是OMIT_XML_DECLARATION

    请注意,在XML1.0中删除标头是有效的,但会丢失字符编码数据(以及其他内容),这可能非常重要

  4. # 4 楼答案

    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));
        }
    
    }