How to use StaX

自古美人都是妖i 提交于 2019-12-02 07:58:37

Your problem has to do w/ basic java programming, nothing to do w/ stax. your FileInputStream is scoped within a try block (some decent code formatting would help) and therefore not visible to the code where you are attempting to create the XMLStreamReader. with formatting:

    XMLInputFactory inFactory = XMLInputFactory.newInstance();
    try {
        // fIS is only visible within this try{} block
        FileInputStream fIS = new FileInputStream("stax.xml");
    } catch (FileNotFoundException na) {
        System.out.println("FileNotFound");
    }
    try {
        // fIS is not visible here
        XMLStreamReader xmlReader = inFactory.createXMLStreamReader(fIS);
    } catch (XMLStreamException xmle) {
        System.out.println(xmle);
    }

on a secondary note, StAX is a nice API, and a great one for highly performant XML processing in java. however, it is not the simplest XML api. you would probably be better off starting with the DOM based apis, and only using StAX if you experience performance issues using DOM. if you do stay with StAX, i'd advise using XMLEventReader instead of XMLStreamReader (again, an easier api).

lastly, do not hide exception details (e.g. catch them and print out something which does not include the exception itself) or ignore them (e.g. continue processing after the exception is thrown without attempting to deal with the problem).

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!