Java变压器空元素

问题描述 投票:0回答:1

我编写了一个程序(我正在使用JDK 11)来格式化XML字符串;但是,我希望我的程序能够收缩空元素。例如:<element></element>应该成为<element/>。我写了下面的代码不起作用:

        Transformer transformer = TransformerFactory.newInstance().newTransformer();
        transformer.setOutputProperty("http://www.oracle.com/xml/is-standalone", "yes");
        transformer.setOutputProperty(OutputKeys.INDENT,"yes");
        transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION,"no");
        transformer.setOutputProperty(OutputKeys.DOCTYPE_PUBLIC,"yes");
        transformer.setOutputProperty(OutputKeys.METHOD,"xml");
        transformer.transform(new DOMSource(unformattedXml),new StreamResult(stringWriter));
        return stringWriter.toString();

我如何收缩空元素?

java xml javax.xml
1个回答
1
投票

我不知道您使用的是哪些XML库。我刚试过原始安装,对我来说很好。

import java.io.StringReader;
import java.io.StringWriter;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
//from w  w  w.  ja  va2 s.c om
import org.w3c.dom.Document;
import org.xml.sax.InputSource;

public class Transform {

    public static void main(String[] args) throws Exception {
        String unformattedXml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
                + "<note>\n"
                + "  <to>example</to>\n"
                + "  <from>Server</from>\n"
                + "  <heading>Reminder</heading>\n"
                + "  <body></body>\n"
                + "</note>";
        Transformer transformer = TransformerFactory.newInstance().newTransformer();
        transformer.setOutputProperty("http://www.oracle.com/xml/is-standalone", "yes");
        transformer.setOutputProperty(OutputKeys.INDENT, "yes");
        transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "no");
        transformer.setOutputProperty(OutputKeys.DOCTYPE_PUBLIC, "yes");
        transformer.setOutputProperty(OutputKeys.METHOD, "xml");
        StringWriter stringWriter = new StringWriter();
        transformer.transform(new DOMSource(convertStringToDocument(unformattedXml).getDocumentElement()), new StreamResult(stringWriter));
        String s = stringWriter.toString();
        System.out.println(s);
    }

    private static Document convertStringToDocument(String xmlStr) {
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        DocumentBuilder builder;
        try {
            builder = factory.newDocumentBuilder();
            Document doc = builder.parse(new InputSource(new StringReader(xmlStr)));
            return doc;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
}

输出:

<?xml version="1.0" encoding="UTF-8"?>
<note>
  <to>example</to>
  <from>Server</from>
  <heading>Reminder</heading>
  <body/>
</note>
© www.soinside.com 2019 - 2024. All rights reserved.