流和实际数据有什么区别?

发布于 2024-10-22 03:51:26 字数 1451 浏览 2 评论 0原文

在java中,有用于输入/输出的流。

我很困惑,当我创建流时,它是流中的数据还是只是数据的管道?

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

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

parser.parseStream(connection.getInputStream());

其中连接是 HttpURLConnection 对象。

以下是 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)

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)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

绻影浮沉 2024-10-29 03:51:26

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

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

然后,您应该将该数据提供给解析器,看看当您提供已知数据时会发生什么。

如果这两种情况都正常的话,就可以直接喂数据了。

[更新 2011-03-12]

这是一个适合我的完整示例。也许您可以发现代码的差异(您只发布了部分代码,尤其是创建 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");
    }
  }

}

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

}
韬韬不绝 2024-10-29 03:51:26

在Java中没有“数据”这样的东西,只有“对象”。与其他事物一样,InputStream 是一个对象。它具有使您可以访问数据的方法,例如 read()。问它是否“是”数据是一个毫无意义的问题 - 面向对象语言的一个原则是数据总是隐藏在接口后面,例如 read() 接口。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文