devOn
devOn

Reputation: 55

How to search an element in xml?

I need to search for an element in an .xml file. It has this structure:

   <elements number="3">
     <contact>
       <name>PAUL</name>
       <code>A1</code>
     </contact>
     <contact>
       <name>LAURA</name>
       <code>A2</code>
     </contact>
     <contact>
       <name>JOHN</name>
       <code>A3</code>
     </contact>

My solution would be so:

public static String readContactsList(String nameContact) {
       XMLInputFactory xmlif = null;
       XMLStreamReader xmlr = null;

       String contacts = "contacts.xml";

       try {
           xmlif = XMLInputFactory.newInstance();
           xmlr = xmlif.createXMLStreamReader(contacts, new FileInputStream(contacts)); 
            while (xmlr.hasNext()) { 
                switch (xmlr.getEventType()) { 
                case XMLStreamConstants.START_DOCUMENT: 
                    System.out.println("Start Read Doc " + contacts); 
                    break;

                case XMLStreamConstants.START_ELEMENT: 
                    System.out.println("Tag " + xmlr.getLocalName());
                    for (int i = 0; i < xmlr.getAttributeCount(); i++)
                        System.out.printf(" => attribut %s->%s%n", xmlr1.getAttributeLocalName(i), xmlr.getAttributeValue(i)); 
                    break;
                case XMLStreamConstants.END_ELEMENT: 
                    System.out.println("END-Tag " + xmlr.getLocalName()); 
                    break;
                case XMLStreamConstants.COMMENT:
                    System.out.println("// comment " + xmlr.getText()); 
                    break; 
                case XMLStreamConstants.CHARACTERS: 
                    if (xmlr.getText().trim().length() > 0) 
                    if (!xmlr.getText().equals(nameContact)) {
                        xmlr.next();
                    }
                    else 
                    System.out.println("-> " + xmlr.getText()); 
                    break;
            }
               xmlr.next();
            }
       } 
       catch (Exception e) {
           System.out.println("reader initialization error");
           System.out.println(e.getMessage()); 
           }
       return contacts;
   }

This method receives a name of one contact (i.e. "LAURA") and it should return its code. What I miss is how to scroll the .xml file and how to access the different tags contents.

Thanks!

Upvotes: 0

Views: 223

Answers (1)

Jucaalpa
Jucaalpa

Reputation: 310

The following code (based on yours) should work, but be aware that if you switch the order of the tags <name> and <code> it will fail.

Also, be aware that you are not closing the stream. Just put a finally {xmlr.close();} that closes the stream.

   public static String readContactsList(String nameContact) {
        boolean match = false;   
        XMLInputFactory xmlif = null;
        XMLStreamReader xmlr = null;

        String contacts = "contacts.xml";

        try {
            xmlif = XMLInputFactory.newInstance();
            xmlr = xmlif.createXMLStreamReader(contacts, new FileInputStream(contacts)); 
            while (xmlr.hasNext()) { 
                switch (xmlr.getEventType()) { 
                case XMLStreamConstants.START_DOCUMENT: 
                    System.out.println("Start Read Doc " + contacts); 
                    break;

                case XMLStreamConstants.START_ELEMENT: 
                    System.out.println("Tag " + xmlr.getLocalName());    
                    for (int i = 0; i < xmlr.getAttributeCount(); i++)
                        System.out.printf(" => attribut %s->%s%n", xmlr.getAttributeLocalName(i), xmlr.getAttributeValue(i));
                    break;

                case XMLStreamConstants.END_ELEMENT: 
                    System.out.println("END-Tag " + xmlr.getLocalName()); 
                    break;

                case XMLStreamConstants.COMMENT:
                    System.out.println("// comment " + xmlr.getText()); 
                    break; 

                case XMLStreamConstants.CHARACTERS: 
                    if (xmlr.getText().trim().length() > 0) {
                        if (!xmlr.getText().equals(nameContact)) {
                            System.out.println("-> " + xmlr.getText());

                            if (match) {
                                return xmlr.getText();
                            }
                        }
                        else {
                            System.out.println("-> " + xmlr.getText());

                            //This is just in case the name and the code be the same
                            if (match) {
                                return xmlr.getText();
                            }

                            match = true;
                        }
                    }
                    break;
                }
                xmlr.next();
            }
        } 
        catch (Exception e) {
            System.out.println("reader initialization error");
            System.out.println(e.getMessage()); 
        }
        return contacts;
    }

Upvotes: 1

Related Questions