2014-05-06 83 views
0

我有一個XML,如: -的Stax解析器問題

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

回答

0

必須調用flush()關閉

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

下面的代碼示例適用於您所附加

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

好的示例XML之前..我做到了......但原問題仍然存在... –

+0

你得到空標籤? –

+0

嗨肯尼斯,我收到一個空文件。沒有數據。 –