try {
    File file = new File("/Users/Alex Ting/Desktop/test2/MovieList.xml");

    DocumentBuilder dBuilder = DocumentBuilderFactory.newInstance().newDocumentBuilder();

    Document doc = dBuilder.parse(file);

    NodeList movielist = doc.getElementsByTagName("movie");
    for(int i =0;i<movielist.getLength(); i++){
        Node m=movielist.item(i);
        if(m.getNodeType()==Node.ELEMENT_NODE){
        Element movie = (Element) m;
        String movieID = movie.getAttribute("id");

        NodeList mnamelist=movie.getChildNodes();
        for(int j=0;j<movielist.getLength(); j++){
        Node n = mnamelist.item(j);
        if(n.getNodeType()==Node.ELEMENT_NODE){
        Element name = (Element) n;
        //System.out.println("Movie ID: "+ movieID +"\n"+ name.getTagName()+ ": "+ name.getTextContent());
        //System.out.println(name.getTagName()+ ": "+ name.getTextContent());
        if(name.getTextContent().equals(smoviename)){
        System.out.println("Movie Id :"+movie.getAttribute("id"));

        System.out.println(name.getTagName()+ ": "+ name.getTextContent());

        }
        }
        }
        }
    }

     } catch (Exception e) {
    e.printStackTrace();
    }

my xml file

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<movies>
    <movie id="1001">
        <title>Blackhat</title>
        <genre>thriller</genre>
        <duration> 90 mins </duration>
    </movie>

    <movie id="1002">
        <title>The Wedding Ringer</title>
        <genre>comedy</genre>
        <duration> 100 mins </duration>
    </movie>
    <movie id="1003">
        <title>The Avengers</title>
        <genre>action</genre>
        <duration> 180 mins </duration>   
    </movie>
    <movie id="1004">
        <title>Taken 3</title>
        <genre>action</genre>
        <duration> 100 mins </duration>
    </movie>

    <movie id="1005">
        <title>Insurgent</title>
        <genre>Science Fiction</genre>
        <duration>180 mins</duration>
    </movie>

    <movie id="1006">
        <title>Jurassic World</title>
        <genre>Adventure</genre>
        <duration>120 mins</duration>
    </movie>

    <movie id="1007">
        <title>Interstellar</title>
        <genre>Science Fiction</genre>
        <duration>180 mins</duration>
    </movie>
</movies>

for example client key in "Interstellar" and the server will receive and get the id value. now my problem is i only can print out the movie id and title only. i need to print out the whole set thing, example, movie id,title, genre and duration. do i need loop for that?

Here's one way:
System.out.println("title = " + movie.getElementsByTagName("title").item(0).getTextContent());

commented: thanks for the help, its working. +2
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.