使用java缩进遍历所有XML标记

时间:2013-08-06 18:45:24

标签: java xml parsing dom iteration

所以,我有以下XML文件 样品:

<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>email1@email.com</ns0:EmailAddress1>
      <ns0:EmailAddress2>email2@email.com</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

所以,基本上,我希望每个父节点的子节点都有一个缩进(在java中为“\ t”)。

至于现在,我有以下代码(带递归):

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);
          }
        }


    }           

对他的任何帮助都将不胜感激!

2 个答案:

答案 0 :(得分:1)

简短形式是:在parseTheTags中添加indentLevel参数。在每次递归调用时,递增indentLevel,并使用它来格式化输出。

编辑更新的代码: 你实际上并没有递归地使用indentLevel;您传递给子呼叫的值indent根本与indentLevel变量无关。另外,您可能不希望将StringBuilder用于递归深度变量 - 对它的更改将在递归调用heirarchy中上下传播。

您的基本通话结构应该与

大致相同
public void parseTags (String indentLevel, Node node) {
  //print out node-specific items at indentLevel
  String childIndent = indentLevel + "\t";

  for (Node n : /*whatever nodes you're recursing to*/) {
    parseTags(childIndent, n);
  }
}

这是一个非常标准的递归构造。在当前节点处执行操作,递增递归深度计数器的值,并根据需要进行递归调用。

答案 1 :(得分:0)

所以你的最终代码应该是

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"));
    Transformer t = TransformerFactory.newInstance().newTransformer();
    t.setOutputProperty(OutputKeys.INDENT, "yes");
    t.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4");
    ByteArrayOutputStream s = new ByteArrayOutputStream();
    t.transform(new DOMSource(document),new StreamResult(s));
    System.out.println(new String(s.toByteArray()));
}