XML XML Print Nodes for Headers

I'll start by saying that I'm not very good at Java or XML.

What I have is the code that prints the child nodes as I need. I didn't write any code, I found it on the help site: Java XML Example . Now, what I need to do this is to remove all traces of specifics. This means that I don't want it to look for "TITLE" or "CD", I want it to know what the first node is and print it. Meaning if the first is CD, then it should print CD, etc. For all nodes. I am planning on using this for multiple XML files and none of them will be the same. This is why it is important not to be specific to one XML and use quotation marks.

To give you an idea of ​​what I want as output at the end, it is basically a CSV file. The header at the top is comma separated and the content below is comma separated. For this I also need a print method that lists all the nodes at the top of the headers. I do not know how to do that.

EDIT: My question is, how do I print tag names (like Title) without hardcoding. I want it to also work with an XML file, which, for example, has nothing to do with CD or music.

//This should be the final result:
Title, Artist, Album,
On Fire, Eminem, Recovery,
Not Afraid, Eminem, Recovery,

//I want this code without hardcoding headers like Title inside it. 
Element fstElmnt = (Element) fstNode;
NodeList fstNmElmntLst = fstElmnt.getElementsByTagName("TITLE");
Element fstNmElmnt = (Element) fstNmElmntLst.item(0);
NodeList fstNm = fstNmElmnt.getChildNodes();
System.out.print(((Node) fstNm.item(0)).getNodeValue()+", ");

      

+3


source to share


2 answers


Code completely independent of the element name:

boolean titlePrinted = false;
Element root = doc.getDocumentElement();
NodeList cdNodes = root.getChildNodes();
for (int i = 0; i < cdNodes.getLength(); i++) {
    Node cdNode = cdNodes.item(i);
    if (cdNode.getNodeType() == Node.ELEMENT_NODE) {
        NodeList cdAttrNodes = cdNode.getChildNodes();
        if (!titlePrinted) {
            for (int j = 0; j < cdAttrNodes.getLength(); j++) {
                Node cdAttrNode = cdAttrNodes.item(j);
                // add this if statement
                if (cdAttrNode.getNodeType() == Node.ELEMENT_NODE) {
                    System.out.print(cdAttrNode.getNodeName() + ",");
                }
            }
            System.out.println("");
            titlePrinted = true;
        }
        for (int j = 0; j < cdAttrNodes.getLength(); j++) {
            Node cdAttrNode = cdAttrNodes.item(j);
            if (cdAttrNode.getNodeType() == Node.ELEMENT_NODE) {
                NodeList attrNodes = cdAttrNode.getChildNodes();
                for (int k = 0; k < attrNodes.getLength(); k++) {
                    Node attrNode = attrNodes.item(k);
                    if (attrNode.getNodeType() == Node.TEXT_NODE) {
                        System.out.print(attrNode.getNodeValue() + ",");
                    }
                }
            }
        }
        System.out.println("");
    }
}

      



Tested as suggested .xml

. My solution works with nodes, not independent elements.

+1


source


The answer to the question. Use getChildNodes()

and iterate over the nodes in the returned list. For each node, you can check if the element is in use with

if (node.getNodeType() == Node.ELEMENT_NODE)

      



And, if true, you can get the element tag name using

Element element = (Element) node; 
String elementName = element.getTagName();

      

+1


source







All Articles