小编tum*_*a25的帖子

添加xml-stylesheet并获得standalone = yes

我在下面的代码中添加了解决方案.

底部的代码就是我所拥有的.我删除了所有标签的创建.

在我得到的xml文件的顶部.<?xml version="1.0" encoding="UTF-8" standalone="no"?>注意,独立是没有的,即使你把它设置为是.

第一个问题:我如何获得独立=是?

我想<?xml-stylesheet type="text/xsl" href="my.stylesheet.xsl"?>在xml文件的第二行添加.

第二个问题:我该怎么做?

一些有用的链接?什么?

DocumentBuilderFactory dbfac = DocumentBuilderFactory.newInstance();  
DocumentBuilder docBuilder = dbfac.newDocumentBuilder();  
Document doc = docBuilder.newDocument();
doc.setXmlStandalone(true);
ProcessingInstruction pi = doc.createProcessingInstruction("xml-stylesheet", "type=\"text/xsl\" href=\"my.stylesheet.xsl\"");

Element root = doc.createElement("root-element");
doc.appendChild(root);
doc.insertBefore(pi, root);    

<cut>  

TransformerFactory transfac = TransformerFactory.newInstance();
transfac.setAttribute("indent-number", new Integer(2));
Transformer trans = transfac.newTransformer();
trans.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "no");
trans.setOutputProperty(OutputKeys.STANDALONE, "yes");
trans.setOutputProperty(OutputKeys.INDENT, "yes");
trans.setOutputProperty(OutputKeys.CDATA_SECTION_ELEMENTS, "name");

FileOutputStream fout = new FileOutputStream(filepath);
BufferedOutputStream bout= new BufferedOutputStream(fout);
trans.transform(new DOMSource(doc), new StreamResult(new OutputStreamWriter(bout, "utf-8")));
Run Code Online (Sandbox Code Playgroud)

java xml xslt

11
推荐指数
1
解决办法
9840
查看次数

让Java使用制表符而不是空格来缩进

我正在创建一个XML文档.我让它缩进使用 TransformerFactory.setAttribute("indent-number", new Integer(2)); Transformer.setOutputProperty(OutputKeys.INDENT, "yes");

是否有可能让Java使用制表符而不是空格来缩进?如何?

java xml indentation

6
推荐指数
1
解决办法
4994
查看次数

标签 统计

java ×2

xml ×2

indentation ×1

xslt ×1