流和实际数据有什么区别? [英] What is difference between a stream and the actual data?

查看:32
本文介绍了流和实际数据有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在java中有输入/输出流.

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 ?

实际上,我正在尝试解析从休息请求创建的 xml 响应到返回 xml 响应的 Web 服务.

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

其中 connection 是一个 HttpURLConnection 对象.

where connection is an HttpURLConnection Object.

以下是 parseStream() 的源码

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




    }


}

我遇到了一个例外:

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

推荐答案

InputStream 是可以从中读取数据的东西.我也可以称它为数据源,但我不会称其为管道.对我而言,管道涉及多个粘在一起的部分.

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.

关于您的解析器错误:在将数据直接提供给解析器之前,您应该将其写入文件或 System.out,以确保某些数据确实到达.

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.

[更新 2011-03-12]

这是一个对我有用的完整示例.也许你可以发现你的代码的不同之处(你只发布了部分,尤其是创建了 InputStream 的部分):

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

}

这篇关于流和实际数据有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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