Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Reading XML, isEndElement is never true

  1. #1
    Junior Member
    Join Date
    Dec 2010
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Reading XML, isEndElement is never true

    the goal here is to read in an xml file and return a list of emailObjects.
    my problem is that the last if statement never fires. this one: ( if (event.isEndElement()).
    it's supposed to add the emailObject element to the list.
    the emailObjects are being correctly instantiated, but they never get added to the list, because if(event.isEndElement()) is never true. So the list returns empty.

    Here is the code and the xml sheet that its reading is at the bottom. i'm sure it's something simple i just can't seem to find it.

        public List<emailObject> readConfig2(String configFile) {
            List<emailObject> items = new ArrayList();
            try {
                 XMLInputFactory inputFactory = XMLInputFactory.newInstance();
                 InputStream in = new FileInputStream(configFile);
                XMLEventReader eventReader = inputFactory.createXMLEventReader(in);
                emailObject server = null;
                while (eventReader.hasNext()) {
                    XMLEvent event = eventReader.nextEvent();
     
                    if (event.isStartElement()) {
                        StartElement startElement = event.asStartElement();
                        // If we have a item element we create a new item
                        if (startElement.getName().getLocalPart().equals("emailObject")) {
                            server = new emailObject();
                            // We read the attributes from this tag and add the date attribute to our object
                            Iterator<Attribute> attributes = startElement.getAttributes();
                            while (attributes.hasNext()) {
                                Attribute attribute = attributes.next();
                                System.out.println("attribute friendlyname is " + attribute.getName().toString());
                                if (attribute.getName().toString().equals(friendlyname));
                                server.setName(attribute.getValue());
                                System.out.println(server.getName() + " printed from server class");
                            }
                        }
     
                        if (event.isStartElement()) {
                            if (event.asStartElement().getName().getLocalPart().equals(address)) {
                                event = eventReader.nextEvent();
                                server.setAddress(event.asCharacters().getData());
                                System.out.println(server.getAddress());
                                continue;
                            }
                        }
     
     
                        if (event.asStartElement().getName().getLocalPart().equals(active)) {
                            event = eventReader.nextEvent();
                            server.setActive(event.asCharacters().getData());
                            continue;
                        }
     
     
     
                    }
                    // If we reach the end of an item element we add it to the list
                    if (event.isEndElement()) {
                        EndElement endElement = event.asEndElement();
                        System.out.println("this end element is " + endElement.getName().getLocalPart().toString());
                        if (endElement.getName().getLocalPart().equals("emailObject")) {
                           // items.addElement(server);
                            items.add(server);
     
                            System.out.println("Added to list");
                        }
                    }
     
                }
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (XMLStreamException e) {
                e.printStackTrace();
            }
            return items;
        }

    here is te xml sheet i'm reading:
    <?xml version="1.0" encoding="UTF-8"?>
     
     
    <root>
     
        <config>
    	<emailObject friendlyname="Angela Txt Msg">
                                     <address>number@vtext.com</address>
                                      <active>true</active>
     
    	</emailObject>
                         <emailObject friendlyname="Angela Email">
                                       <address>angelah@emaill.org</address>
                                        <active>true</active>
                          </emailObject>
     
        </config>
     
     
    </root>
    Last edited by Angela; December 12th, 2010 at 01:10 AM. Reason: formatting


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Reading XML, isEndElement is never true

    I don't see any obvious problems. Are there any exceptions being thrown at runtime? I mention this because if I strip it down to bare essentials it works for me
     	        try {
    	             XMLInputFactory inputFactory = XMLInputFactory.newInstance();
    	             InputStream in = new FileInputStream(configFile);
    	            XMLEventReader eventReader = inputFactory.createXMLEventReader(in);
     
    	            while (eventReader.hasNext()) {
    	                XMLEvent event = eventReader.nextEvent();
     
    	                if (event.isStartElement()) {
     
    	                }
    	                // If we reach the end of an item element we add it to the list
    	                if (event.isEndElement()) {
    	                    EndElement endElement = event.asEndElement();
    	                    System.out.println("this end element is " + endElement.getName().getLocalPart().toString());
    	                    if (endElement.getName().getLocalPart().equals("emailObject")) {
     
     
    	                        System.out.println("Added to list");
    	                    }
    	                }
     
    	            }
    	        } catch (FileNotFoundException e) {
    	            e.printStackTrace();
    	        } catch (XMLStreamException e) {
    	            e.printStackTrace();
    	        }

  3. #3
    Junior Member
    Join Date
    Dec 2010
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Reading XML, isEndElement is never true

    The only exception that I get is after this function returns, when i try to use the list becuase the list it returns is empty.

    The last System.out.println never prints, because the code never goes into that if block.

    Thanks for looking at it i was starting to feel kind of crazy. i know how it *should* work, it's just not doing it! When I get some time today I'm going to try to rewrite the xml file. Maybe there's an invisible ascii character in there throwing things off. That's about all i can come up with. Thanks again

Similar Threads

  1. Reading a file
    By Soccer13 in forum Java Theory & Questions
    Replies: 2
    Last Post: October 26th, 2010, 08:55 PM
  2. would this expression be true or false??
    By robertsbd in forum Java Theory & Questions
    Replies: 1
    Last Post: October 24th, 2010, 10:00 PM
  3. Replies: 4
    Last Post: April 27th, 2010, 01:18 AM
  4. Does this still hold true?
    By April in forum File I/O & Other I/O Streams
    Replies: 2
    Last Post: February 2nd, 2010, 09:28 AM
  5. Reading from RSS server
    By Tisofa in forum File I/O & Other I/O Streams
    Replies: 2
    Last Post: August 18th, 2009, 04:04 AM

Tags for this Thread