所以缩进所有的XML标签,我有以下XML文件 样本:迭代通过使用Java
<Entry>
<ns0:entity-Person>
<ns0:Cell>333-333-3333</ns0:CellPhone>
<ns0:DOB>1970-01-01T01:00:00-05:00</ns0:DateOfBirth>
<ns0:FN>Raymond</ns0:FirstName>
<ns0:Gender>M</ns0:Gender>
</ns0:entity-Person>
<ns0:EmailAddress1>[email protected]</ns0:EmailAddress1>
<ns0:EmailAddress2>[email protected]</ns0:EmailAddress2>
<ns0:Entry>
<ns1:OfficialIDType>SSN</ns1:OfficialIDType>
<ns1:OfficialIDValue>342-56-8729</ns1:OfficialIDValue>
</ns0:Entry>
.. ..
,我想下面的输出:
Entry
ns0:entity-Person
ns0:CellPhone
ns0:DateOfBirth
ns0:FN
ns0:Gender
ns0:EmailAddress1
ns0:EmailAddress2
ns0:Entry
ns1:OfficialIDType
ns1:OfficialIDValue
所以,基本上,我想为每个父节点的子节点有一个缩进(“\ t”在java中)。
至于现在,我有以下代码(递归):
public static void main(String[] args) throws SAXException, IOException,
ParserConfigurationException, TransformerException {
DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();
Document document = docBuilder.parse(new File("C:\\sub.xml"));
parseTheTags(document.getDocumentElement());
}
public static void parseTheTags(Node node) {
System.out.println(node.getNodeName());
NodeList nodeList = node.getChildNodes();
for (int i = 0; i < nodeList.getLength(); i++) {
Node currentNode = nodeList.item(i);
if (currentNode.getNodeType() == Node.ELEMENT_NODE) {
parseTheTags(currentNode);
}
}
}
我也知道如何做到这一点没有递归,但它是我无法做到的缩进。 我知道它会在代码的某个地方发生一些小的变化,但我已经花了相当长的时间在这个上,没有用。
这就是我认为stackoverflow可能可以帮助我!
编辑的代码:现在,附加选项卡每个子节点:输出有问题,虽然
public class NewParseXMLTags {
public static void main(String[] args) throws SAXException, IOException,
ParserConfigurationException, TransformerException {
DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();
Document document = docBuilder.parse(new File("C:\\Users\\parasv1\\Desktop\\Self\\sub.xml"));
StringBuilder tmp = new StringBuilder();
tmp.append("");
parseTheTags(tmp, document.getDocumentElement());
}
public static void parseTheTags(StringBuilder indentLevel, Node node) {
StringBuilder indent = new StringBuilder();
System.out.println(indentLevel+node.getNodeName());
NodeList nodeList = node.getChildNodes();
for (int i = 0; i < nodeList.getLength(); i++) {
Node currentNode = nodeList.item(i);
if (currentNode.getNodeType() == Node.ELEMENT_NODE) {
if (currentNode.hasChildNodes())
{
indent.append("\t");
parseTheTags(indent, currentNode);
}
}
}
}
}
答案找到: 于是,经过一些好的想法和Sbodd帮助下,我找到了修复:非常简单!
public class ParseXML {
public static void main(String[] args) throws SAXException, IOException,
ParserConfigurationException, TransformerException {
DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();
Document document = docBuilder.parse(new File("C:\\Users\\parasv1\\Desktop\\Self\\sub.xml"));
String tmp = new String();
tmp = "";
parseTags(tmp, document.getDocumentElement());
}
public static void parseTags (String indentLevel, Node node) {
//print out node-specific items at indentLevel
System.out.println(indentLevel+node.getNodeName());
String childIndent = indentLevel + "\t";
NodeList nodeList = node.getChildNodes();
for (int i = 0; i < nodeList.getLength(); i++) {
Node n = nodeList.item(i);
if (n.getNodeType() == Node.ELEMENT_NODE) {
parseTags(childIndent, n);
}
}
}
任何与他的帮助将不胜感激!
任何人都可以帮忙吗?我仍然没有任何帮助来解决我的问题! :( – VictorCreator