在java中解析xml文件后如何调整输出?

h4cxqtbf  于 2021-07-08  发布在  Java
关注(0)|答案(1)|浏览(289)

我正在尝试使用递归函数来解析xml文档,我不确定什么样的数据形式(或者我应该说如何保存解析的文档,以什么形式)才能轻松地将其转换为json。这是代码:

private static void printNodeList(NodeList nodeList)
{

    for (int count = 0; count < nodeList.getLength(); count++) //getLength of nodeList that is passed inside of function printNodeList
    {
        Node elemNode = nodeList.item(count); //assign name of element we are currently looking at to a Node "elemNode"
        if (elemNode.getNodeType() == Node.ELEMENT_NODE) //if node we are currently looking at has type ELEMENT_NODE do next block of a code
        {
            // get node name and value
            System.out.println("\nNode Name =" + elemNode.getNodeName()+ " [OPEN]");
            System.out.println("Node Content =" + elemNode.getTextContent());

            if (elemNode.hasAttributes()) //if node we are currently looking at has attributes then do this block of a code
            {
                NamedNodeMap nodeMap = elemNode.getAttributes(); //create nodeMap and pass it attributes of a node we are currently looking at
                for (int i = 0; i < nodeMap.getLength(); i++) //getLength of nodeMap or look how many attributes there is for node that we are currently watching
                {
                    Node node = nodeMap.item(i); //every attribute that is stored in nodeMap will be saved in node and his name and value will be printed out
                    System.out.println("attr name : " + node.getNodeName());
                    System.out.println("attr value : " + node.getNodeValue());
                }
            }
            if (elemNode.hasChildNodes()) //if our element (element that we are currently watching at) still has child nodes left -> recursively call "printNodeList" function
            {
                //recursive call if the node has child nodes
                printNodeList(elemNode.getChildNodes());
            }
            //output in console the name of node that we are done with
            System.out.println("Node Name =" + elemNode.getNodeName()+ " [CLOSE]");
        }
    }
}

代码中使用的xml文件,这是我得到的输出。我不确定我怎么会丢失第一个“node content=…”,因为没有它,它会更干净,可能更容易处理数据?
其思想是解析xml文件,然后保存它,然后使用一组转换规则将保存的文件转换为json。或者有没有一种方法可以直接转换为json而不需要保存解析的xml文件?也许我把xml转换成json的整个方法都错了?救命啊。

7vhp5slm

7vhp5slm1#

我有点想知道如何修改输出,但它仍然缺少一些关键点,以json格式表示,所以如果有人有一些想法,请随时分享。任何帮助都很好,谢谢。我可以通过以下方法调整输出:

public static void printNode(NodeList nList) {
    //System.out.println(nList.getLength());
    //nList.getLength() gives us cont of <> and </> under that class

    for (int i = 0; i < nList.getLength(); i++) {
        Node node = nList.item(i);

        if (node.hasChildNodes()) { //check if node has childNodes
            System.out.println("\"" + node.getNodeName() + "\":");
        }
            if(node.hasAttributes()){ //check if node have any attributes
                System.out.println("->"+node.getAttributes().item(0));
                //getAttributes() is going to get a list of attributes and we want only first one that's why we put "0" as a parameter
            }
            if(node.getChildNodes().getLength()==1){
                //if childNode length is equal to 1 that means this is a leaf node (latest node in the tree)
                System.out.println("---->"+node.getTextContent());
            }
            //recursive call
        printNode(node.getChildNodes());

    }
}

对于这个输入,我可以得到这个输出。我想试着创造一些更像这样的东西。

相关问题