来自javax.xml.transform.Transformer的漂亮打印输出,只有标准的java api(缩进和Doctype定位) [英] Pretty-printing output from javax.xml.transform.Transformer with only standard java api (Indentation and Doctype positioning)

查看:182
本文介绍了来自javax.xml.transform.Transformer的漂亮打印输出,只有标准的java api(缩进和Doctype定位)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用以下简单代码:

package test;

import java.io.*;
import javax.xml.transform.*;
import javax.xml.transform.stream.*;

public class TestOutputKeys {
    public static void main(String[] args) throws TransformerException {

        // Instantiate transformer input
        Source xmlInput = new StreamSource(new StringReader(
                "<!-- Document comment --><aaa><bbb/><ccc/></aaa>"));
        StreamResult xmlOutput = new StreamResult(new StringWriter());

        // Configure transformer
        Transformer transformer = TransformerFactory.newInstance()
                .newTransformer(); // An identity transformer
        transformer.setOutputProperty(OutputKeys.DOCTYPE_SYSTEM, "testing.dtd");
        transformer.setOutputProperty(OutputKeys.INDENT, "yes");
        transformer.transform(xmlInput, xmlOutput);

        System.out.println(xmlOutput.getWriter().toString());
    }

}

我得到输出:

<?xml version="1.0" encoding="UTF-8"?>
<!-- Document comment --><!DOCTYPE aaa SYSTEM "testing.dtd">

<aaa>
<bbb/>
<ccc/>
</aaa>

问题A:doctype标签出现在文档注释之后。是否可以使它出现在文档注释之前?

Question A: The doctype tag appears after the document comment. Is it possible to make it appear before the document comment?

问题B:如何仅使用JavaSE 5.0 API实现缩进?
这个问题基本上与如何从java中打印xml相同然而该问题中的几乎所有答案都取决于外部库。唯一适用的答案(由名为Lorenzo Boccaccia的用户发布)仅使用java的api,基本上与上面发布的代码相同,但对我不起作用(如输出所示,我没有缩进)。

Question B: How do I achieve indentation, using only the JavaSE 5.0 API? This question is essentially identical to How to pretty-print xml from java, however almost all answers in that question depend on external libraries. The only applicable answer (posted by a user named Lorenzo Boccaccia) which only uses java's api, is basically equal to the code posted above, but does not work for me (as shown in the output, i get no indentation).

我猜你必须设置用于缩进的空格量,因为许多外部库的答案都有,但我找不到在哪里指定java api。鉴于在java api中存在将缩进属性设置为yes的可能性,必须能够以某种方式执行缩进。我只是无法弄清楚如何。

I am guessing that you have to set the amount of spaces to use for indentation, as many of the answers with external libraries do, but I just cannot find where to specify that in the java api. Given the fact that the possibility to set an indentation property to "yes" exists in the java api, it must be possible to perform indentation somehow. I just can't figure out how.

推荐答案

缺少的部分是缩进金额。您可以设置缩进和缩进量,如下所示:

The missing part is the amount to indent. You can set the indentation and indent amount as follow:

transformer.setOutputProperty(OutputKeys.INDENT, "yes");
transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");
transformer.transform(xmlInput, xmlOutput);

这篇关于来自javax.xml.transform.Transformer的漂亮打印输出,只有标准的java api(缩进和Doctype定位)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆