Reputation: 487
I'll start off by saying I'm not very good with Java or XML.
What I have below is code that prints out the child nodes just like I need. I did not write the code, I found it on a help site here: Java XML Example. Now, what I need for it to do is remove all traces of specificity. Meaning that I don't want it to search "TITLE" or "CD", I want it to find out what the first node is and print it. Meaning if the first is CD, then it should print CD, and so on for all of the nodes. I plan on using this for multiple XML files and none of them are the same. Which is why it's important not to be specific to one XML and use quotes.
To give you a perspective of what I want as an output at the end, is basically a CSV file. Header at the top, seperated by commas and content below, seperated by commas. For this to work, I also need a print method that lists all of the nodes at the top for the headers. I'm unsure of how to do this.
EDIT: My question is, how do I print the tag names (ex. Title) without hardcoding it. I want it to also work an XML file that has nothing to do with CD's or music for instance.
//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()+", ");
Upvotes: 3
Views: 1426
Reputation: 8229
Totally element-name-independent code:
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 on your suggested .xml
. My solution works with nodes instead of elements to be independent.
Upvotes: 1
Reputation: 692121
The answer is in the question. Use getChildNodes()
, and iterate through the nodes in the returned list. For each node, you can check if it's an element using
if (node.getNodeType() == Node.ELEMENT_NODE)
And, if true, you can get the element's tag name using
Element element = (Element) node;
String elementName = element.getTagName();
Upvotes: 1