Lar*_*ars 7 java xml stax indentation
我正在使用StAX创建一个非常大的xml文档.到目前为止,我使用IndentingXMLStreamwriter类来获取格式良好的文档(另请参阅此答案).几天前,我们设置了一个jenkins服务器,它有一个较旧的jdk版本(6.26),我得到了构建错误.
package com.sun.xml.internal.txw2.output does not exist
Run Code Online (Sandbox Code Playgroud)
我认为由于安装了jdk版本,无法找到包.由于不同的原因,这不能改变(顺便说一下,有没有人知道jdk版本,这个包(com.sun.xml.internal.txw2.output)被添加了?).
因此,我正在寻找缩进的替代方案.我更喜欢类似于我使用的解决方案,这意味着无需重新分析文档.任何想法或建议?
谢谢
Lars
小智 11
而不是com.sun.xml.internal.txw2.output.IndentingXMLStreamWriter使用com.sun.xml.txw2.output.IndentingXMLStreamWriter可以在以下位置找到:
<dependency>
<groupId>org.glassfish.jaxb</groupId>
<artifactId>txw2</artifactId>
<version>2.2.11</version>
</dependency>
Run Code Online (Sandbox Code Playgroud)
只是扩展Michael Kay的答案(/sf/answers/707601401/).
maven依赖:
<dependency>
<groupId>net.sf.saxon</groupId>
<artifactId>Saxon-HE</artifactId>
<version>9.6.0-5</version>
</dependency>
Run Code Online (Sandbox Code Playgroud)
java代码:
import java.io.OutputStream;
import net.sf.saxon.Configuration;
import net.sf.saxon.s9api.Processor;
import net.sf.saxon.s9api.SaxonApiException;
import net.sf.saxon.s9api.Serializer;
import net.sf.saxon.s9api.Serializer.Property;
public class Main {
public static void main(String[] args) throws Exception {
OutputStream outputStream = System.out;
writeXmlDocument(outputStream);
}
private static void writeXmlDocument(OutputStream outputStream){
Configuration config = new Configuration();
Processor processor = new Processor(config);
Serializer serializer = processor.newSerializer();
serializer.setOutputProperty(Property.METHOD, "xml");
serializer.setOutputProperty(Property.INDENT, "yes");
serializer.setOutputStream(outputStream);
try {
XMLStreamWriter writer = serializer.getXMLStreamWriter();
try {
writer.writeStartDocument();
{
writer.writeStartElement("root_element_name");
{
writer.writeStartElement("child_element");
writer.writeEndElement();
}
writer.writeEndElement();
}
writer.writeEndDocument();
writer.flush();
writer.close();
} catch (XMLStreamException e) {
e.printStackTrace();
}
} catch (SaxonApiException e) {
e.printStackTrace();
}
}
}
Run Code Online (Sandbox Code Playgroud)
如果其他建议不起作用,您可以从 Saxon 获取缩进 XMLStreamWriter,如下所示:
Processor p = new net.sf.saxon.s9api.Processor();
Serializer s = p.newSerializer();
s.setOutputProperty(Property.METHOD, "xml");
s.setOutputProperty(Property.INDENT, "yes");
s.setOutputStream(....);
XMLStreamWriter writer = s.getXMLStreamWriter();
Run Code Online (Sandbox Code Playgroud)
一个优点是,这允许您使用其他序列化属性对序列化进行大量控制。
| 归档时间: |
|
| 查看次数: |
9170 次 |
| 最近记录: |