Hi guys

I have a tricky XML/Java situation that I need some tips on
I have an XML file
<country>
<country-name code=”USA”>
<city>City1</city>
<city>City2</city>
<city>City3</city>
</country-name>

<country-name code=”FRA”>
<city>City1</city>
<city>City2</city>
<city>City3</city>
</country-name>

</country >

Firstly I need to count how many cities per country. I know I have to get the parent and child nodes and so on
Secondly I need to display these cities names per country (if code =”FRA”)'

How do I do this in Java?
Thanks in advance!

Dani AI

Generated

Good starting points from and — they pointed you toward XML tooling. Two concise, practical ways to solve both parts of your task follow: (A) bind the XML to Java objects and iterate to count/list cities, or (B) run an XPath query to count or select city nodes directly. Both are simple to apply; below are minimal examples and a few gotchas to watch for.

Example: bind with JAXB (POJOs)

@XmlRootElement(name="country")
public class Countries {
  @XmlElement(name="country-name")
  private List<CountryName> countryName;
  // getters/setters
}

public class CountryName {
  @XmlAttribute(name="code")
  private String code;
  @XmlElement(name="city")
  private List<String> city;
  // getters/setters
}

// unmarshal + use
JAXBContext jc = JAXBContext.newInstance(Countries.class);
Countries countries = (Countries) jc.createUnmarshaller().unmarshal(new File("countries.xml"));

for (CountryName cn : countries.getCountryName()) {
  System.out.println(cn.getCode() + " -> " + cn.getCity().size()); // count per country
}

countries.getCountryName().stream()
  .filter(cn -> "FRA".equals(cn.getCode()))
  .findFirst()
  .ifPresent(cn -> cn.getCity().forEach(System.out::println)); // list for code="FRA"

Alternative: XPath (no binding)

XPath xp = XPathFactory.newInstance().newXPath();
Double count = (Double) xp.evaluate("count(/country/country-name[@code='FRA']/city)", doc, XPathConstants.NUMBER);

NodeList nodes = (NodeList) xp.evaluate("/country/country-name[@code='FRA']/city", doc, XPathConstants.NODESET);
for (int i=0;i<nodes.getLength();i++) System.out.println(nodes.item(i).getTextContent());

Notes and quick troubleshooting

  • Make sure the XML is well formed (use straight ASCII quotes for attributes).
  • Hyphenated element names like country-name require explicit name mapping in annotations (see @XmlElement(name="country-name")).
  • If the app runs on Java 11 or newer, JAXB classes are no longer bundled in the JDK — add the JAXB API and an implementation (for example jakarta.xml.bind:jakarta.xml.bind-api plus a runtime like com.sun.xml.bind:jaxb-impl or org.glassfish.jaxb:jaxb-runtime). (openjdk.org)

Either approach will let you produce the per-country counts and lists quickly; choose binding if you prefer typed objects, or XPath if you want a compact query-based solution.

Recommended Answers

All 2 Replies

Hi michael.ngobeni,

Have a read of the javax.xml classes in the Java API. You should look in particular at the parsers package. Another step might be the java xml tutorials on sun.com.

Anyway I hope this has given you a starting point. Post back if you need more help.

Cheers
darkagn

yah... java's xml parsers can help you more... couple of them r, DOM, JDOM, SAX...
if u use DOM or JDOM, get the whole document parsed, then just use a method called getElementsByTagName(String Tag). This is pretty easy to do. you will have to follow factory pattern.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.