Java Reference
In-Depth Information
doc.getElementsByTagName("contact");
for (int i = 0; i < contacts.getLength(); i++)
{
Element contact = (Element) contacts.item(i);
NodeList
cities
=
con-
tact.getElementsByTagName("city");
boolean chicago = false;
for (int j = 0; j < cities.getLength(); j++)
{
Element city = (Element) cities.item(j);
NodeList children = city.getChildNodes();
StringBuilder sb = new StringBuilder();
for (int k = 0; k < children.getLength();
k++)
{
Node child = children.item(k);
if
(child.getNodeType()
==
Node.TEXT_NODE)
sb.append(child.getNodeValue());
}
if (sb.toString().equals("Chicago"))
{
chicago = true;
break;
}
}
if (chicago)
{
NodeList
names
=
con-
tact.getElementsByTagName("name");
con-
tactNames.add(names.item(0).getFirstChild().getNodeValue());
}
}
for (String contactName: contactNames)
System.out.println(contactName);
Search WWH ::




Custom Search