使用System.out.println()迭代XML输入流

时间:2014-09-09 21:34:55

标签: java xml

我将xml文件导入InputStream,然后尝试使用jaxb解组它。我想读出输入流中的内容,以便我可以更好地诊断后续步骤。如何遍历输入流中的xml内容?

以下是需要迭代InputStream的方法:

public static <T> T unmarshal( Class<T> docClass, InputStream inputStream )throws JAXBException {
    String packageName = docClass.getPackage().getName();
    JAXBContext jc = JAXBContext.newInstance( packageName );
    Unmarshaller u = jc.createUnmarshaller();
    int r;
    try {
        while ((r = inputStream.read()) > 0) {
        //how do i iterate the inputstream?
        //System.out.println() for each line in the xml file
        }
    } catch (IOException e) {e.printStackTrace();}
    Object object = u.unmarshal( inputStream );//this throws error.  I want to see what is inside
    System.out.println(object.getClass());
    System.out.println(JAXBIntrospector.getValue(object).getClass());
    JAXBElement<T> doc = (JAXBElement<T>)object;//u.unmarshal( inputStream );
    return doc.getValue();
}  

只是为了踢,调用上述方法的代码是:

public static void main( String[] args ) {
    String filename = "C:\\Temp\\jaxb\\apps\\create-marshal\\po.xml";
    InputStream is = getInputStream(filename);
    try { unmarshal(BusinessDocument.class, is);} 
    catch (JAXBException e) {e.printStackTrace();}
}

public static InputStream getInputStream(String filename){
    InputStream is = null;
    try {
        is = new FileInputStream(filename);
    }
    catch (FileNotFoundException e) {e.printStackTrace();}
    catch (IOException e) {e.printStackTrace();}
    return is;
}  

修改

控制台中运行@ Rp代码的输出如下:

<?xml version="1.0"?>
<businessDocument>
    <typeId root="docroot1" extension="docext1"/>
    <documentTitle>first document</documentTitle>
    <entity1>
        <typeId root="innerroot1" extension="innerext1"/>
        <entityTitle>inner title</entityTitle>
    </entity1>
</businessDocument>
javax.xml.bind.UnmarshalException
 - with linked exception:
[org.xml.sax.SAXParseException: Premature end of file.]
    at javax.xml.bind.helpers.AbstractUnmarshallerImpl.createUnmarshalException(AbstractUnmarshallerImpl.java:315)
    at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallerImpl.createUnmarshalException(UnmarshallerImpl.java:505)
    at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal0(UnmarshallerImpl.java:206)
    at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal(UnmarshallerImpl.java:173)
    at javax.xml.bind.helpers.AbstractUnmarshallerImpl.unmarshal(AbstractUnmarshallerImpl.java:137)
    at javax.xml.bind.helpers.AbstractUnmarshallerImpl.unmarshal(AbstractUnmarshallerImpl.java:184)
    at mainpackage.Main.unmarshal(Main.java:44)
    at mainpackage.Main.main(Main.java:18)
Caused by: org.xml.sax.SAXParseException: Premature end of file.
    at com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.createSAXParseException(ErrorHandlerWrapper.java:195)
    at com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.fatalError(ErrorHandlerWrapper.java:174)
    at com.sun.org.apache.xerces.internal.impl.XMLErrorReporter.reportError(XMLErrorReporter.java:388)
    at com.sun.org.apache.xerces.internal.impl.XMLScanner.reportFatalError(XMLScanner.java:1414)
    at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl$PrologDriver.next(XMLDocumentScannerImpl.java:1059)
    at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl.next(XMLDocumentScannerImpl.java:648)
    at com.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl.next(XMLNSDocumentScannerImpl.java:140)
    at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanDocument(XMLDocumentFragmentScannerImpl.java:511)
    at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:808)
    at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:737)
    at com.sun.org.apache.xerces.internal.parsers.XMLParser.parse(XMLParser.java:119)
    at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.parse(AbstractSAXParser.java:1205)
    at com.sun.org.apache.xerces.internal.jaxp.SAXParserImpl$JAXPSAXParser.parse(SAXParserImpl.java:522)
    at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal0(UnmarshallerImpl.java:200)
    ... 5 more

1 个答案:

答案 0 :(得分:1)

通常,您可以使用BufferedReader来读取流。像

这样的东西
BufferedReader in = new BufferedReader(new InputStreamReader(inputStream));
String line = null;
while ((line = in.readLine()) != null) {
    System.out.println(line);
}