Stax解析器问题

时间:2014-05-06 05:53:49

标签: java xml-parsing stax

我有一个XML: -

<?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>

我想从上面的XML中准备另一个XML,如下所示: -

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

这主要是为了从XML中提取一些特定标记以准备另一个XML文档。 我正在使用以下Java代码段使用Stax解析器。但它产生的输出是一个空白文件。 output.xml为空。

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){}
    }
}

1 个答案:

答案 0 :(得分:0)

您必须在结束前致电flush()

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

以下代码示例适用于您附加的示例xml

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();
    }
  }
}