简体   繁体   English

XML到CSV转换的大子节点

[英]XML to CSV conversion grand child nodes

Sorry if this question is already asked, I had searched different solutions on the website but did not find an appropriate answer for my problem. 抱歉,如果已经提出此问题,我在网站上搜索了其他解决方案,但没有找到适合我问题的合适答案。 I have XML like 我有像XML

<Book>
        <Action>NEW</Action>
        <Attributes>
            <Attribute>
                <AttributeName>SwapBook</AttributeName>
                <AttributeValue>A</AttributeValue>
            </Attribute>
            <Attribute>
                <AttributeName>PricerKey</AttributeName>
                <AttributeValue>TRADING</AttributeValue>
            </Attribute>
        </Attributes>
    </Book>

I tried to write the code for converting the XML to a flat CSV file for returning all the header names. 我试图编写用于将XML转换为平面CSV文件以返回所有标头名称的代码。 But my code is only working for the items at first level. 但是我的代码仅适用于第一级的项目。

private List<String> getHeaders(Document document) {
    Element root = document.getDocumentElement();
    NodeList list = root.getChildNodes();
    List<String> headers = new ArrayList<String>();

    for (int i=0; i< list.getLength(); i++) {
        if (list.item(i).getNodeType() == Node.ELEMENT_NODE) {
            Node node = list.item(i);
            NodeList nodeList = node.getChildNodes();
            int children = nodeList.getLength();
            System.out.println("children="+children);
            for (int k = 0; k < children; k++) {
                if (nodeList.item(k).getNodeType() == Node.ELEMENT_NODE) {
                    headers.add(nodeList.item(k).getNodeName());
                }
            }
            break;
    }
}
    return headers;
}

I had seen a post on stack overflow How to get only the top level node's text content with getTextContent() which explains why I'm unable to reach the grand child/child level header names/values. 我看到过一篇有关堆栈溢出的文章,其中介绍了如何使用getTextContent()仅获取顶级节点的文本内容,这解释了为什么我无法获得子级/子级头的大名/值。 I'm able to print only the Action and Attribute , but the Attribute and AttributeName are not visible. 我只能打印ActionAttribute ,但是AttributeAttributeName不可见。

Any help is sincerely appreciated. 真诚的感谢您的帮助。 Thanks in advance. 提前致谢。

try this, it will loop through all the children of all nodes and child nodes, 尝试此操作,它将遍历所有节点和子节点的所有子节点,

private List<String> getHeaders(Document document) {
    Element root = document.getDocumentElement();
    NodeList list = root.getChildNodes();
    List<String> headers = new ArrayList<String>();

    for (int i=0; i< list.getLength(); i++) {
        if (list.item(i).getNodeType() == Node.ELEMENT_NODE) {
            Node node = list.item(i);
            addNode(node, headers);
            break;
    }
}
    return headers;
}

private void addNode(Node node, List<String> headers)
{
  NodeList nodeList = node.getChildNodes();
  if(nodeList==null||nodeList.Length==0)return;
  int children = nodeList.getLength();
  for (int k = 0; k < children; k++) {
      if (nodeList.item(k).getNodeType() == Node.ELEMENT_NODE) {
          headers.add(nodeList.item(k).getNodeName());
          addNode(nodeList.item(k), headers);
      }
  }
}

I had come up with the following solution like checking for the child nodes recursively. 我想出了以下解决方案,例如递归检查子节点。 Please suggest me if I can improve the code in some way. 如果可以以某种方式改进代码,请提出建议。

private void checkChildNodes(Node root){
    if(root.hasChildNodes()){
        NodeList list = root.getChildNodes();
        int length = list.getLength();
        for(int i = 0; i < length; i++){
            if (list.item(i).getNodeType() == Node.ELEMENT_NODE) {
                System.out.println(list.item(i).getNodeName());
                checkChildNodes(list.item(i));
            }
        }
    }
    else{
        System.out.println(root.getNodeName());
    }
}

Thanks. 谢谢。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM