java StAX - StartDocument的独立属性

时间:2015-02-20 13:05:51

标签: java xml-parsing stax

我想阅读,操作和编写xml文件。我想从刚读取和写入开始,然后再操作。

我使用StAX Parser并希望使用EventReader和EventWriter。

我想在读取和编写StartDocument元素时遇到第一个问题。

输入:

<?xml version="1.0" encoding="iso-8859-15" standalone="yes"?>

文件输出&#34; data_output_test.xml&#34;:

<?xml version="1.0" encoding="iso-8859-15"?>

我使用

显式创建了startDocument对象
eventFactory.createStartDocument("iso-8859-15", "1.0", true);

但是当我将新的startDocument对象写入文件时,该属性不显示。

这是我的代码:

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;

import javax.xml.stream.XMLEventFactory;
import javax.xml.stream.XMLEventReader;
import javax.xml.stream.XMLEventWriter;
import javax.xml.stream.XMLInputFactory;
import javax.xml.stream.XMLOutputFactory;
import javax.xml.stream.XMLStreamConstants;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.events.XMLEvent;

public class XML_ReadWrite{

    @SuppressWarnings("unchecked")
    public static void main(String[] args) {
        XMLInputFactory factory_in = null;
        XMLOutputFactory factory_out = null;
        XMLEventReader  eventReader = null;
        XMLEventFactory  eventFactory = null;
        XMLEventWriter eventWriter = null;
        XMLEvent event_out = null;
        XMLEvent event_in = null;

        try {
            factory_in = XMLInputFactory.newInstance();
            eventReader  = factory_in.createXMLEventReader (getReader("data_test.xml", "iso-8859-15"));
        } catch (XMLStreamException e) {
            e.printStackTrace();
            return;
        } catch (Exception e) {
            //UnsupportedEncodingException or FileNotFoundException
            e.printStackTrace();
            return;
        }

        try {
            factory_out = XMLOutputFactory.newInstance();
            eventFactory = XMLEventFactory.newInstance();
            eventWriter = factory_out.createXMLEventWriter(getWriter("data_output_test.xml", "iso-8859-15"));
            event_out = eventFactory.createStartDocument(null, "1.0", true);
            eventWriter.add(event_out);
            eventWriter.flush();
        } catch (XMLStreamException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (Exception e) {
            //UnsupportedEncodingException or FileNotFoundException
            e.printStackTrace();
            return;
        }

        while(eventReader.hasNext()){

            try {
            event_in = eventReader.nextEvent();
            } catch (XMLStreamException e) {
                e.printStackTrace();
            continue;
            }

            if(event_in.getEventType() == XMLStreamConstants.START_DOCUMENT){
                System.out.println("StartElement: " + event_in.isStartDocument());
                System.out.println("StartElement: " + event_in.toString());             
            }
        }       
    }


    public static InputStreamReader getReader(String filename, String encoding) throws Exception {
        FileInputStream in = new FileInputStream(filename);
        InputStreamReader r = null;
        r = new InputStreamReader(in, encoding);
        return r;
    }

    public static OutputStreamWriter getWriter(String filename, String encoding) throws Exception {
        FileOutputStream out = new FileOutputStream(filename);
        OutputStreamWriter w = new OutputStreamWriter(out, encoding);
        return w;
    }
}

有没有办法强制XMLEventWriter编写独立属性呢?

我的第二个问题是,当我这样做时,无法识别编码

event_in.toString()

关于事件startDocument。

我的节目输出是:

StartElement: true
StartElement: <?xml version="1.0" encoding='null' standalone='yes'?>

为什么?

提前致谢

0 个答案:

没有答案