Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get element name from XML in Java DOM

Tags:

java

dom

xml

I want to get the element name and print the data in the XML, but not sure how to get data under specific element.

Here is the XML sample and my code.

   <mdb>
    <movies>
    <movie id="godfather">
      <title>The Godfather</title>
      <year>1972</year>
      <directors>
        <director idref="francisfordcoppola"/>
      </directors>
      <genres>
        <genre>Crime</genre>
        <genre>Drama</genre>
      </genres>
       <cast>
        <performer>
          <actor idref="marlonbrando"/>
          <role>Don Vito Corleone</role>
        </performer>
     </cast>
    </movie>
    </movies>

    <performer id="kimnovak">
      <name>Marilyn Pauline Novak</name>
      <dob>1933-02-13</dob>
      <pob>Chicago, Illinois, USA</pob>
      <actedin>
        <movie idref="vertigo"/>
      </actedin>
    </performer>
    </mdb>



try {
        File fXmlFile = new File(filename);
        DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
        DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
        Document doc = dBuilder.parse(fXmlFile);

        NodeList nodes = doc.getElementsByTagName("movie");
         System.out.println("nodes length"+ nodes.getLength());
        for (int i = 0; i < nodes.getLength(); i++){
            Element element = (Element) nodes.item(i);
            NodeList name = element.getElementsByTagName("title");
            Element line = (Element) name.item(0);
            System.out.println(": " + line.getFirstChild().getTextContent());

I only want to get the element inside movie. but the following code also reads the <movie idref="vertigo"/> inside the performer(causes Nullpointer exception if I want to get the content); I'm wondering if there is any possible way to avoid read the under Performance by using DOM?

NodeList nodes = doc.getElementsByTagName("movie");

My final output for the first movie should look like this

('godfather', 'The Godfather', '1972', 'Crime;Drama')
like image 469
Michael Avatar asked Feb 07 '13 03:02

Michael


1 Answers

One way to do this is to start reading from movies tag instead of the movie tag. Not sure if this is what you are looking for !!.

NodeList nodes = doc.getElementsByTagName("movies");

Element element = (Element) nodes.item(0);
NodeList movieList = element.getElementsByTagName("movie");
for (int i = 0; i < movieList.getLength(); i++) {
    Element movieElement = (Element) movieList.item(i);
    System.out.println(movieElement.getAttributes().getNamedItem("id").getNodeValue());
    NodeList name = movieElement.getElementsByTagName("title");
    NodeList year = movieElement.getElementsByTagName("year");
    NodeList genres = movieElement.getElementsByTagName("genres");
    Element genreline = (Element) genres.item(0);

    System.out.println(name.item(0).getFirstChild().getTextContent());
    System.out.println(year.item(0).getFirstChild().getTextContent());
    System.out.println(genreline.getElementsByTagName("genre").item(0).getTextContent() 
               + ":" + genreline.getElementsByTagName("genre").item(1).getTextContent());
}

Output :

: godfather
: The Godfather
: 1972
: Crime:Drama

like image 181
Ramadas Avatar answered Sep 22 '22 02:09

Ramadas