Java 标识XML的内部节点

Java 标识XML的内部节点,java,xml,xslt,Java,Xml,Xslt,我使用: 从这个问题,但正如有人所说:它并没有像我们期望的那样识别内部节点(它确实识别它们,但不是用4个空格) 第一级识别有4个空格,下一级识别有2个空格,如: transformer.setOutputProperty(OutputKeys.INDENT, "yes"); transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4"); 如果原始输入XML本身已缩进,则输出程序添加的缩进看起来不正

我使用:

从这个问题,但正如有人所说:它并没有像我们期望的那样识别内部节点(它确实识别它们,但不是用4个空格)

第一级识别有4个空格,下一级识别有2个空格,如:

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

如果原始输入XML本身已缩进,则输出程序添加的缩进看起来不正确。在重新缩进之前,您可以尝试使用简单的XSLT去掉原始XML中的任何缩进,而不是无操作标识转换器:

DocumentBuilderFactory documentFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder documentBuilder = documentFactory.newDocumentBuilder();
Document doc = documentBuilder.parse(xmlFile);
doc.getDocumentElement().normalize();

NodeList rootlist = doc.getElementsByTagName("root_node"); //(example name)
Node root = rootlist.item(0);

root.appendChild(...);

TransformerFactory transformerFactory = TransformerFactory.newInstance();
Transformer transformer = transformerFactory.newTransformer();

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

DOMSource source = new DOMSource(doc);
StreamResult result = new StreamResult(new File("output.xml"));

transformer.transform(source, result);
TransformerFactory TransformerFactory=TransformerFactory.newInstance();
Source xsltSource=新的StreamSource(新的StringReader(
“\n”
+“\n”
+“\n”
+“\n”
+ ""
));
Transformer Transformer=transformerFactory.newTransformer(xsltSource);
DOMSource=新的DOMSource(doc);
StreamResult=newstreamresult(新文件(“output.xml”);
变换(源、结果);

缩进时使用了多少空格?当您不提供信息时,它不会帮助我们回答问题。它使用2(我认为这类似于默认设置)。(第一级标识用4个空格,第二级标识用2个空格。)@BobDalgleish我按照您的要求对其进行了编辑。我怀疑在序列化程序获得数据以添加自己的缩进之前,转换生成的输出树中存在一些仅限空白的文本节点。序列化程序添加的缩进只有在没有额外空格的情况下才看起来合理。您的输入XML和XSLT是什么样子的-您是否在输入上执行
xsl:strip space
?@IanRoberts我将源代码放在了最后。非常有效。有没有关于你答案的更多信息的指南/网站?谢谢大家!@user2692669重要的一位是
xsl:strip space
,如中所述。
<a>
 (4)<b>
   (2)<b_sub></b_sub>
 (4)</b>
 (4)<c></c>
(2)</a>
DocumentBuilderFactory documentFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder documentBuilder = documentFactory.newDocumentBuilder();
Document doc = documentBuilder.parse(xmlFile);
doc.getDocumentElement().normalize();

NodeList rootlist = doc.getElementsByTagName("root_node"); //(example name)
Node root = rootlist.item(0);

root.appendChild(...);

TransformerFactory transformerFactory = TransformerFactory.newInstance();
Transformer transformer = transformerFactory.newTransformer();

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

DOMSource source = new DOMSource(doc);
StreamResult result = new StreamResult(new File("output.xml"));

transformer.transform(source, result);
TransformerFactory transformerFactory = TransformerFactory.newInstance();
Source xsltSource = new StreamSource(new StringReader(
  "<xsl:stylesheet xmlns:xsl='http://www.w3.org/1999/XSL/Transform' version='1.0'\n"
+ "    xmlns:xalan='http://xml.apache.org/xalan'>\n"
+ "  <xsl:strip-space elements='*' />\n"
+ "  <xsl:output method='xml' indent='yes' xalan:indent-amount='4' />\n"
+ "  <xsl:template match='/'><xsl:copy-of select='node()' /></xsl:template>\n"
+ "</xsl:stylesheet>"
));
Transformer transformer = transformerFactory.newTransformer(xsltSource);

DOMSource source = new DOMSource(doc);
StreamResult result = new StreamResult(new File("output.xml"));

transformer.transform(source, result);