[英]Java Transformer Empty Element
I wrote a program (I'm using JDK 11) to format an XML string; 我编写了一个程序(我正在使用JDK 11)来格式化XML字符串。 however, I want my program to contract empty elements.
但是,我希望程序收缩空元素。 For example:
<element></element>
should become <element/>
. 例如:
<element></element>
应该成为<element/>
。 I have written the below code which does not work: 我写了下面的代码不起作用:
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();
How can I contract the empty elements? 如何缩小空白元素?
I don't know which XML libraries you are using. 我不知道您正在使用哪个XML库。 I just tried with raw installation and works fine for me.
我只是尝试使用原始安装,并且对我来说效果很好。
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;
}
}
Output: 输出:
<?xml version="1.0" encoding="UTF-8"?>
<note>
<to>example</to>
<from>Server</from>
<heading>Reminder</heading>
<body/>
</note>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.