所以縮進所有的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