如何过滤XML标记,然后在动态生成的文件中编写过滤的标记

时间:2012-03-27 15:38:41

标签: java xml stax

我有这个Java代码:

public static void main(String[] args) throws Exception {

    String filename = "testXML.xml";
    String fileOutputName = "output.xml";
    try
    {           
        XMLInputFactory xmlif = null;
        xmlif = XMLInputFactory.newInstance();
        xmlif.setProperty(XMLInputFactory.IS_REPLACING_ENTITY_REFERENCES,Boolean.TRUE);
        xmlif.setProperty(XMLInputFactory.IS_SUPPORTING_EXTERNAL_ENTITIES,Boolean.FALSE);
        xmlif.setProperty(XMLInputFactory.IS_NAMESPACE_AWARE, Boolean.TRUE);
        xmlif.setProperty(XMLInputFactory.IS_COALESCING, Boolean.TRUE);
        xmlif.setProperty(XMLInputFactory.REPORTER,"");
        FileInputStream fis = new FileInputStream(filename);
        XMLStreamReader xmlr = xmlif.createFilteredReader(xmlif.createXMLStreamReader(fis),new xmlReader());            

        OutputStream outputFile = new FileOutputStream(fileOutputName);                     
        XMLOutputFactory outputFactory = XMLOutputFactory.newInstance();            
        XMLStreamWriter xmlWriter = outputFactory.createXMLStreamWriter(outputFile);    

        while (xmlr.hasNext())          
        {               
            write(xmlr, xmlWriter);             
            xmlr.next();
        }
        //write(xmlr, xmlWriter);                                   
        xmlWriter.flush();
        xmlWriter.close();          
        xmlr.close();
        outputFile.close();         
    }
    catch (Exception e)
    {
        e.printStackTrace();
    }
}

public boolean accept(XMLStreamReader reader) {     
    filters logic
    return true;
}

public static void write(XMLStreamReader xmlr, XMLStreamWriter writer) throws XMLStreamException {
    switch (xmlr.getEventType()) {
    case XMLEvent.START_ELEMENT:            
        final String localName = xmlr.getLocalName();
        writer.writeStartElement(localName);    
    break;
    case XMLEvent.END_ELEMENT:          
        writer.writeEndElement();
    break;
    case XMLEvent.SPACE:
    break;
    case XMLEvent.CHARACTERS:           
        writer.writeCharacters(xmlr.getTextCharacters(), xmlr.getTextStart(), xmlr.getTextLength());                    
        break;
    case XMLEvent.PROCESSING_INSTRUCTION:           
        writer.writeProcessingInstruction(xmlr.getPITarget(), xmlr.getPIData());            
    break;
    case XMLEvent.CDATA:                
            writer.writeCData(xmlr.getText());              
    break;
    case XMLEvent.COMMENT:
        writer.writeComment(xmlr.getText());
    break;
    case XMLEvent.ENTITY_REFERENCE:
        writer.writeEntityRef(xmlr.getLocalName());
    break;
    case XMLEvent.START_DOCUMENT:           
        String encoding = xmlr.getCharacterEncodingScheme();
        String version = xmlr.getVersion();
        if (encoding != null && version != null)
            writer.writeStartDocument(encoding, version);
        else if (version != null)
            writer.writeStartDocument(xmlr.getVersion());               
    break;          
    case XMLEvent.END_DOCUMENT:         
        writer.writeEndDocument();          
    break;
    case XMLEvent.DTD:
        writer.writeDTD(xmlr.getText());            
    break;
    }
  }

解析这样的XML:

<xml>
<element id="1">
    <field>Some field</field>
    <field2>Some other field value</field2>
    <comment>This will be ignored, so whatever.</comment>
</element>
<element id="2">
    <field>Value for field 1</field>
    <field2>Value for field 2</field2>
    <comment>Blah, blah, blah</comment>
</element>
...
<element id="n">
    <field>Value for field n</field>
    <field2>Value for field n</field2>
    <comment>Comment may be as long as a politician speech</comment>
</element></xml>

基本上我是通过标签过滤文件并将代码写入XML文件。

现在,我遇到的麻烦就是当我尝试做这样的事情时:

while (xmlr.hasNext())          
        {
            XMLStreamWriter xmlWriter = XMLOutputFactory.newInstance().createXMLStreamWriter(new FileOutputStream(currentFile));
            write(xmlWriter);
            xmlr.next();
            xmlWriter.close();
        }           
        xmlr.close();

currentFile正在被更改,当我运行它时,它给了我一个例外,说明第一个关闭元素无法弹出。我要做的是过滤标签,然后当标签更改时将其写入为该标签设计的文件中。

这是一个例外:

java.util.EmptyStackException
at com.bea.xml.stream.util.Stack.pop(Stack.java:XX)
at com.bea.xml.stream.XMLWriterBase.writeEndElement(XMLWriterBase.java:XX)
at xmlReader.write(xmlReader.java:XX)
at xmlReader.main(xmlReader.java:XX)

有谁知道如何解决这个问题?

感谢。

更新1 有没有人知道每次关闭编写器时XMLWriter的实现是否删除了所有推送的值?

0 个答案:

没有答案