To parse a XML file using Java, we need two Java libraries, the org.w3c.dom and javax.xml.parsers. There are _ steps to do
Read the xml file to a org.w3c.dom.Document:
Now, we get a doc object for performing our xml parsing task.
We need to normalise the document by:
The normalise method has the following comments in the library:
What this actually means is that it will help you normalise all the text node. For example, if you have a node like:
Before you get your document normalised, it can be represent by:
After you normalise the doc, it is:
From now on, you can search or perform other tasks against your doc object.
Get a list of targeted Node. This can be done by calling the method getElementsByTagName(String tagName). For example, when we want all the note element in our test.xml and iterate through each one: