Java Reference
In-Depth Information
}
catch (IOException ioe)
{
System.err.println("IOE: "+ioe);
}
catch (SAXException saxe)
{
System.err.println("SAXE: "+saxe);
}
catch (FactoryConfigurationError fce)
{
System.err.println("FCE: "+fce);
}
catch (ParserConfigurationException pce)
{
System.err.println("PCE: "+pce);
}
}
}
After parsing contacts.xml and building the DOM tree, main() uses Docu-
ment 's getElementsByTagName() methodtoreturna NodeList of contact
element nodes. For each member of this list, main() extracts the contact element
node,andusesthisnodewith getElementsByTagName() toreturna NodeList
of the contact element node's city element nodes.
For each member of the cities list, main() extracts the city element node,
andusesthisnodewith getElementsByTagName() toreturna NodeList ofthe
city elementnode'schildnodes—thereisonlyasinglechildtextnodeinthisexample,
butthepresenceofacommentorprocessinginstructionwouldincreasethenumberof
childnodes.Forexample, <city>Chicago<!--The windy city--></city>
increases the number of child nodes to 2.
Ifthechild'snodetypeindicatesthatitisatextnode,thechildnode'svalue(obtained
via getNodeValue() )isstoredinastringbuilder—onlyonechildnodeisstoredin
thestringbuilderinthisexample. Ifthebuilder'scontentsindicate that Chicago has
been found, the chicago flag is set to true and execution leaves the cities loop.
Search WWH ::




Custom Search