What is difference between a stream and the actual

2019-09-19 02:38发布

In java there are streams for input/output.

I am confused that when i create a stream, is it the data that is in the stream or just the pipeline for the data ?

Actually i am trying to parse an xml response created from a rest request to a web service that returns an xml response.

//Parse Xml
ParseXml parser=new ParseXml();

parser.parseStream(connection.getInputStream());

where connection is an HttpURLConnection Object.

Following is the source for parseStream()

import java.io.IOException;
import java.io.InputStream;
import java.io.Reader;

import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.XMLReader;
import org.xml.sax.helpers.XMLReaderFactory;

public class ParseXml 
{
    public void parseStream(InputStream input)
    {
        XMLReader xmlReader;

        try 
        {
            xmlReader = (XMLReader) XMLReaderFactory.createXMLReader();
            xmlReader.setContentHandler(new XmlParser());

            xmlReader.parse(new InputSource(input));
        } 
        catch (SAXException e) 
        {
            e.printStackTrace();
        } 
        catch (IOException e) 
        {
            e.printStackTrace();
        }




    }


}

I'm getting an exception :

[Fatal Error] :1:1: Premature end of file.
org.xml.sax.SAXParseException: Premature end of file.
    at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.parse(Unknown Source)
    at xmlparsing.ParseXml.parseStream(ParseXml.java:24)
    at smssend.SmsSend.restHttpPost(SmsSend.java:129)
    at main.SmsApiClass.main(SmsApiClass.java:28)

2条回答
神经病院院长
2楼-- · 2019-09-19 02:52

In Java there's no such thing as "data", there are only "objects". Like everything else, an InputStream is an object. It has methods, such as read(), that give you access to data. Asking whether it "is" the data is a meaningless question - a principle of object-oriented languages is that data is always hidden behind interfaces, such as the read() interface.

查看更多
ゆ 、 Hurt°
3楼-- · 2019-09-19 02:54

An InputStream is something from which you can read data. I could also call it a data source, but I wouldn't call it a pipeline. To me a pipeline involves multiple parts that are sticked together.

Regarding your parser error: Before feeding the data directly to the parser, you should write it to a file or System.out, just to make sure that some data actually arrived.

Then you should feed that data to the parser, to see what happens when you feed it known data.

And if these two cases work properly, you can feed the data directly.

[Update 2011-03-12]

Here is a complete example that works for me. Maybe you can spot the difference to your code (of which you only posted parts, especially not the part that creates the InputStream):

package so5281746;

import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;

import org.xml.sax.Attributes;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.XMLReader;
import org.xml.sax.helpers.DefaultHandler;
import org.xml.sax.helpers.XMLReaderFactory;

public class ParseXml {

  public static void parseStream(InputStream input) {
    try {
      XMLReader xmlReader = XMLReaderFactory.createXMLReader();
      xmlReader.setContentHandler(new XmlParser());
      xmlReader.parse(new InputSource(input));
    } catch (SAXException e) {
      e.printStackTrace();
    } catch (IOException e) {
      e.printStackTrace();
    }

  }

  public static void main(String[] args) throws IOException {
    URLConnection conn = new URL("http://repo1.maven.org/maven2/org/apache/ant/ant/maven-metadata.xml").openConnection();
    InputStream input = conn.getInputStream();
    parseStream(input);
  }

  static class XmlParser extends DefaultHandler {

    @Override
    public void startDocument() throws SAXException {
      System.out.println("startDocument");
    }

    @Override
    public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
      System.out.println("startElement " + localName);
    }

    @Override
    public void endDocument() throws SAXException {
      System.out.println("endDocument");
    }
  }

}
查看更多
登录 后发表回答