Search code examples
javaparsingstax

StAX JAVA pulling out specific tags and data


I am trying to understand how to identify and specific XML and extract a specific piece of data at one of those tags.

As I try to get to the above goal, I have been going through the docs and examples and then branching off and modifying.

I am using StAX

My entire code and xml file is at the bottom of this post.

I have 2 questions: 1) I have a question on why part of my code is not behaving the way I thought it would. I have

String elem = se.getName().toString();
System.out.printf("elem = %s\n",elem);
if( se.getName().toString() == "{http://www.publishing.org}Date")
                        //if( elem == "1")
                        {
                            System.out.println("Here !!!!!!!!!!!!!!!!!");
                        }

My System.out.printf(“elem = %s\n”,elem);

yields: elem = {http://www.publishing.org}Date

But my if statement if( se.getName().toString() == "{http://www.publishing.org}Date") Is never true, meaning I never get the “Here !!!”

Question 2, Why am I getting:

{http://www.publishing.org}author
{http://www.publishing.org}Date
{http://www.publishing.org}ISBN

Instead of just author, date, and ISBN? Why is every line also giving me {http://publishing.org}?

public static void main(String[] args) throws FileNotFoundException, XMLStreamException
{
    // TODO code application logic here
    //System.out.println("Here");
    //String filename = null;
    String filename = "BookCatalog.xml";
    XMLInputFactory factory = XMLInputFactory.newInstance();
    XMLEventReader reader = factory.createXMLEventReader(new FileReader(filename));
    while(reader.hasNext())
            {
                XMLEvent event = reader.nextEvent();
                XMLEvent nextEvent = reader.peek();
                switch (event.getEventType())
                        {
                    case XMLEvent.START_ELEMENT:
                        StartElement se = event.asStartElement();
                        //System.out.println("Here");
                        //System.out.print("<" + se.getName());

                        System.out.print(" " + se.getName());
                        System.out.printf("\n");
                        String elem = se.getName().toString();
                        //String elem = "1";
                        System.out.printf("elem = %s\n",elem);
                        //String ele = event.getAttributeName();
                        if( se.getName().toString() == "{http://www.publishing.org}Date")
                        //if( elem == "1")
                        {
                            System.out.println("Here !!!!!!!!!!!!!!!!!");
                        }
                        Iterator attributes = se.getNamespaces();

                        while(attributes.hasNext())
                        {
                            Attribute attr= (Attribute)attributes.next();
                            System.out.print(" " + attr.getName() + "=\"" +attr.getValue() +"\"");
                            System.out.printf("\n");
                        }//end while loop
                    System.out.print(">");
                        if(nextEvent.isCharacters())
                        {
                            Characters c = reader.nextEvent().asCharacters();
                            if(!c.isWhiteSpace())
                            System.out.print(c.getData());
                            System.out.printf("\n");


                        }// end if
                    /*case XMLEvent.END_ELEMENT>
                        EndElement ee = event.asEndElement();
                        System.out.print("</"+ee.getName()+">");
                        break;
                        * */
                        }// end witch
            }// end while
    reader.close();
}//end Main

and the XML: http://www.publishing.org"> Yogasana Dhirenda 1966 81-40 Dhirenda 11.50 Yogasana J. K 1954 0-06 Harper 2.95


Solution

  • Question 1: this looks like .equals() vs ==. I think you want .equals().