如何使用StaX [英] How to use StaX

查看:151
本文介绍了如何使用StaX的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嘿伙计们,所以我是Java-XML解析世界的新手,发现StaX API可能是我最好的选择,因为我需要读取和写入XML文件。好吧,所以我有一个非常简短的(应该是非常简单的)程序(应该)创建一个XMLInputFactory并使用它来创建XMLStreamReader。 XMLStreamReader是使用附加到与源文件位于同一目录中的XML文件的FileInputStream创建的。但是,即使正确编译了FileInputStream,XMLInputFactory也无法访问它,如果没有FileInputStream,它就无法创建XMLStreamReader。请帮忙,因为我不知道该放弃了什么并且感到沮丧!

Hey guys so I am brand new to the world of Java-XML parsing and found that the StaX API is probably my best bet as I need to both read and write XML files. Alright so I have a very short (and should be very simple) program that (should) create an XMLInputFactory and use that to create a XMLStreamReader. The XMLStreamReader is created using a FileInputStream attached to an XML file in the same directory as the source file. However even though the FileInputStream compiled properly, the XMLInputFactory cannot access it and without the FileInputStream it cannot creat the XMLStreamReader. Please help as I have no idea what to and am frustrated to the point of giving up!

import javax.xml.stream.*;
import java.io.*;
public class xml {
    static String status;
    public static void main(String[] args) {
        status = "Program has started";
        printStatus();      
        XMLInputFactory inFactory = XMLInputFactory.newInstance();
            status = "XMLInputFactory (inFactory) defined"; printStatus();
        try { FileInputStream fIS = new FileInputStream("stax.xml"); }
            catch (FileNotFoundException na) { System.out.println("FileNotFound"); }
            status = "InputStream (fIS) declared"; printStatus();
        try { XMLStreamReader xmlReader = inFactory.createXMLStreamReader(fIS); } catch (XMLStreamException xmle) { System.out.println(xmle); }
            status = "XMLStreamReader (xmlReader) created by 'inFactory'"; printStatus();
    }
    public static void printStatus(){ //this is a little code that send notifications when something has been done
        System.out.println("Status: " + status);
    }
}

如果需要,这里也是XML文件:

also here is the XML file if you need it:

<?xml version="1.0"?>
    <dennis>
        <hair>brown</hair>
        <pants>blue</pants>
        <gender>male</gender>
    </dennis>


推荐答案

你的问题必须要做基本的java编程,与stax无关。您的FileInputStream在try块中范围内(一些不错的代码格式化会有所帮助),因此您尝试创建XMLStreamReader的代码不可见。格式化:

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

在二级音符上,StAX是一个不错的API,非常适合java中高性能的XML处理。但是,它不是最简单的 XML api。你最好从基于DOM的apis开始,如果你遇到使用DOM的性能问题,那么只能使用StAX。如果你留在StAX,我建议使用 XMLEventReader 而不是 XMLStreamReader (再次,一个更简单的api)。

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).

这篇关于如何使用StaX的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆