Search code examples
javaxml-parsingstax

Stax parser issue


I have an XML like:-

<?xml version="1.0" encoding="UTF-8" ?> 
<BookCatalogue xmlns="http://www.publishing.org">
    <Book>
        <Title>Yogasana Vijnana: the Science of Yoga</Title> 
        <Author>Dhirendra Brahmachari</Author> 
        <Date>1966</Date> 
        <ISBN>81-40-34319-4</ISBN> 
        <Publisher>Dhirendra Yoga Publications</Publisher> 
        <Cost currency="INR">11.50</Cost> 
    </Book>
</BookCatalogue>

I want to prepare another XML from the above XML as follows:-

<?xml version="1.0" encoding="UTF-8" ?>
<BookCatalogue xmlns="http://www.publishing.org">
    <Title>Yogasana Vijnana: the Science of Yoga</Title> 
</BookCatalogue>

This is mainly for the purpose of extracting some specifc tags from an XML to prepare another XML document. I am using the below Java snippet using Stax parser. But the output it is producing is a blank file. output.xml is blank.

public class GMLRead {

    public static String filename="BookCatalogue.xml";
    public static String outputfile="output.xml";
    public GMLRead(){}
    public static void main(String args[]) throws XMLStreamException,IOException
    {
    int openElements = 0;
    boolean firstRun = true;
    try 
    {
    XMLEventFactory m_eventFactory = XMLEventFactory.newInstance();
    GMLRead gr=new GMLRead(); 
    XMLInputFactory xmlif = XMLInputFactory.newInstance();
    XMLOutputFactory xmlof = XMLOutputFactory.newInstance();
    XMLEventReader reader = XMLInputFactory.newInstance().createXMLEventReader(new java.io.FileInputStream(filename));
    XMLEventWriter writer = XMLOutputFactory.newInstance().createXMLEventWriter(new FileWriter(outputfile));
    while(reader.hasNext())
        {
        XMLEvent event= reader.nextEvent();
        int event_type=event.getEventType();
        if (event.isStartElement()) 
            {
            if (event.asStartElement().getName().getLocalPart().equalsIgnoreCase("Title"))
                {
                    writer.add(event);
                }
            }
        if (event.isEndElement()) 
            {
            if (event.asEndElement().getName().getLocalPart().equalsIgnoreCase("Title"))
                {
                    writer.add(event);
                }
            }

        }
    writer.close();
    }
    catch(Exception e){}
    }
}

Solution

  • You must call flush() before closing

    writer.flush();
    writer.close();
    

    The following code example works with the example xml you have attached

    public class TestRead
    {
      public static String filename = "sampleXML.xml";
      public static String outputfile = "output.xml";
    
      public static void main(String args[]) throws XMLStreamException, IOException
      {
        try
        {
          XMLEventReader reader = XMLInputFactory.newInstance().createXMLEventReader(new     java.io.FileInputStream(filename));
          XMLEventWriter writer = XMLOutputFactory.newInstance().createXMLEventWriter(new     FileWriter(outputfile));
          while (reader.hasNext())
          {
            XMLEvent event = reader.nextEvent();
            if (event.isStartElement())
            {
              if(event.asStartElement().getName().getLocalPart().equalsIgnoreCase("Title"))
              {
                writer.add(event);
                XMLEvent xmlEvent = reader.nextEvent();
                writer.add(xmlEvent);
              }
            }
            if (event.isEndElement())
            {
              if (event.asEndElement().getName().getLocalPart().equalsIgnoreCase("Title"))
              {
                writer.add(event);
              }
            }
          }
          writer.flush(); 
          writer.close();
        }
        catch (Exception e)
        {
          e.printStackTrace();
        }
      }
    }