Bump.
So To update from my BB app problem, I finally figured out how parse the XML. It would work in Java as well, and you'll most likely have to parse XML at some point.
I pretty much assume that you know NOTHING about XML and you just need to know the basics. This method would work with any well written markup language (HTML) as well.
An XML file formats its data (text) in a nested structure between tags(called elements). The data
AND the tags are called
nodes. For example
Code:
<!--this is some sample xml-->
<name>Jonothan</name>
"<name>" is an
element node
"Jonothan" is
text node
<!--this is some sample text --> is a
comment node, But dont worry about that for now.
There's two ways to parse XML in Java. There's the
DOM method (
Document
Object Model) which I will be showing you, where the entire XML is loaded into a tree format (basically how you see the text in the file) and allows you to pick and choose what parts of the document you want. Its a lot more efficient and allows you to modify the XML, and but is uses a lot of memory since it loads the entire document.
Then there's the
SAX method which is useless, omits chunks of data, and is a waste of time.
Now, to traverse the XML file is pretty tedious, as you have to work your way through every Element node for the text nodes you want. For example:
Code:
<!--Example #1, lets call it, sample.xml-->
<atrl>
<posterName>
Big Smoke
</posterName>
</atrl>
First you call a DocumentBuilderFactory object, and a DocumentBuilder to build your file.
Code:
public class parseXML{
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
}
Then you create a new
Document object, and use that to call the
parse() method in DocumentBuilder, which takes your XML file as a parameter.
Code:
public class parseXML{
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
Document doc = dBuilder.parse(sample.xml);
}
Now to create a
NodeList object using the Document's
getElementsByTagName(String tagName) method. A NodeList is basically like an array. Its a list of nodes, whether they are element nodes or text nodes, that are within the node you specify,
That are NOT nested in other element tags.
Like with our sample XML, if we specify <atrl> as our tag name in the getElementsByTagName method, the NodeList would consist of only <posterName> and not "Jonothan." You would have create another NodeList object and pass "posterName" as the parameter in the method.
Code:
public class parseXML{
public static void main(String[]a){
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
Document doc = dBuilder.parse(sample.xml);
NodeList node = doc.getElementByTagName("atrl");
}
}
To iterate through a NodeList is just like iterating through a regular array with a for loop instantiate an int, set to increment till its value is less than the actual length of the NodeList, print the values.
The names of the nodes are stored in a method called
getNodeName()
The values of the nodes are stored in a method called
getNodeValue() and the actual position of the nodes in the NodeList is called by the
item(int position) method, in the Node object. Everything in a NodeList is a Node object, by the way.
Note: Element tags DON'T have values, so calling getNodeValue() on an element node will return null.
Text Nodes DON'T have names, so calling getNodeValue() on a text node will return this :#text
which is as good as null aka pretty useless
Code:
public class parseXML{
public static void main(String[]a){
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
Document doc = dBuilder.parse(sample.xml);
NodeList node = doc.getElementByTagName("atrl");
for(int i=0; i<node.getLength(); i++){
System.out.println(node.item(i).getNodeValue());
System.out.println(node.item(i).getNodeValue());
}
}
}
The output of the code could be
Hope it helps somebody. If there's anything wrong let me know.
In my next post I'll show you how to get deeply nested values (Or at least how I did it, anyway)