Problem reading zipped XML document

Discussion in 'Java' started by NB, Jun 1, 2004.

  1. NB

    NB Guest

    I'm trying to read the following (very simple) zipped XML document:

    <?xml version="1.0" encoding="iso-8859-1"?>
    <data>
    <value>123</value>
    </data>

    The problem is that I'm getting the following error:

    Error on line 5: Illegal XML character: .

    The problem must be with the unpacking since I get no error when reading
    the manually unpacked XML document with the same code.
    Is there someone who can help? I'd grealy appreciate some help as I'm
    getting rather frustrated :)

    ---CODE---

    import org.jdom.input.SAXBuilder;
    import org.jdom.Element;
    import org.jdom.Document;
    import org.jdom.JDOMException;

    import java.util.zip.ZipInputStream;
    import java.util.zip.ZipEntry;
    import java.net.URL;
    import java.io.*;

    public class ZIP {
    final static int BUFFER_SIZE = 1024;

    public static void main(String[] args) {
    try {
    // Gets file from website.
    URL url = new URL("http://localhost/website/zip.file");
    InputStream webInputStream = url.openStream();

    System.out.println("Debug: Done getting file from website.");

    // Unpacks file.
    ZipInputStream zipInputStream = new ZipInputStream
    (webInputStream);
    ZipEntry zipEntry;

    int byteCount;
    byte buffer[] = new byte[BUFFER_SIZE];

    ByteArrayOutputStream byteArrayOutputStream = new
    ByteArrayOutputStream();

    while ((zipEntry = zipInputStream.getNextEntry()) != null) {
    while ((byteCount = zipInputStream.read(buffer, 0, BUFFER_SIZE))
    != -1) {
    byteArrayOutputStream.write(buffer, 0, BUFFER_SIZE);
    }
    }

    zipInputStream.close();

    System.out.println("Debug: Done unpacking file.");

    ByteArrayInputStream byteArrayInputStream = new
    ByteArrayInputStream(byteArrayOutputStream.toByteArray());

    // Reads XML document.
    SAXBuilder saxBuilder = new SAXBuilder();

    try {
    //-----> The error occurs at the following line <-----//
    Document document = saxBuilder.build(byteArrayInputStream);
    Element rootElement = document.getRootElement();

    if (rootElement != null) {
    String value = rootElement.getChild("value").getText();
    System.out.println(value);
    }
    }
    catch (JDOMException e) {
    System.out.println(e.getMessage());
    }

    System.out.println("Debug: Done reading XML-document.");
    } catch(Exception e) {
    e.printStackTrace();
    }
    }
    }
    NB, Jun 1, 2004
    #1
    1. Advertising

  2. NB

    Liz Guest

    You only show lines 1-4 yet the error is on line 5
    How can we tell?????



    "NB" <> wrote in message
    news:...
    > I'm trying to read the following (very simple) zipped XML document:
    >
    > <?xml version="1.0" encoding="iso-8859-1"?>
    > <data>
    > <value>123</value>
    > </data>
    >
    > The problem is that I'm getting the following error:
    >
    > Error on line 5: Illegal XML character: .
    >
    > The problem must be with the unpacking since I get no error when reading
    > the manually unpacked XML document with the same code.
    > Is there someone who can help? I'd grealy appreciate some help as I'm
    > getting rather frustrated :)
    >
    > ---CODE---
    >
    > import org.jdom.input.SAXBuilder;
    > import org.jdom.Element;
    > import org.jdom.Document;
    > import org.jdom.JDOMException;
    >
    > import java.util.zip.ZipInputStream;
    > import java.util.zip.ZipEntry;
    > import java.net.URL;
    > import java.io.*;
    >
    > public class ZIP {
    > final static int BUFFER_SIZE = 1024;
    >
    > public static void main(String[] args) {
    > try {
    > // Gets file from website.
    > URL url = new URL("http://localhost/website/zip.file");
    > InputStream webInputStream = url.openStream();
    >
    > System.out.println("Debug: Done getting file from website.");
    >
    > // Unpacks file.
    > ZipInputStream zipInputStream = new ZipInputStream
    > (webInputStream);
    > ZipEntry zipEntry;
    >
    > int byteCount;
    > byte buffer[] = new byte[BUFFER_SIZE];
    >
    > ByteArrayOutputStream byteArrayOutputStream = new
    > ByteArrayOutputStream();
    >
    > while ((zipEntry = zipInputStream.getNextEntry()) != null) {
    > while ((byteCount = zipInputStream.read(buffer, 0, BUFFER_SIZE))
    > != -1) {
    > byteArrayOutputStream.write(buffer, 0, BUFFER_SIZE);
    > }
    > }
    >
    > zipInputStream.close();
    >
    > System.out.println("Debug: Done unpacking file.");
    >
    > ByteArrayInputStream byteArrayInputStream = new
    > ByteArrayInputStream(byteArrayOutputStream.toByteArray());
    >
    > // Reads XML document.
    > SAXBuilder saxBuilder = new SAXBuilder();
    >
    > try {
    > //-----> The error occurs at the following line <-----//
    > Document document = saxBuilder.build(byteArrayInputStream);
    > Element rootElement = document.getRootElement();
    >
    > if (rootElement != null) {
    > String value = rootElement.getChild("value").getText();
    > System.out.println(value);
    > }
    > }
    > catch (JDOMException e) {
    > System.out.println(e.getMessage());
    > }
    >
    > System.out.println("Debug: Done reading XML-document.");
    > } catch(Exception e) {
    > e.printStackTrace();
    > }
    > }
    > }
    >
    Liz, Jun 1, 2004
    #2
    1. Advertising

  3. On Tue, 01 Jun 2004 18:59:33 GMT, Liz wrote:
    > "NB" <> wrote in message

    ...
    >> <?xml version="1.0" encoding="iso-8859-1"?>
    >> <data>
    >> <value>123</value>
    >> </data>
    >>
    >> The problem is that I'm getting the following error:
    >>
    >> Error on line 5: Illegal XML character: .


    > You only show lines 1-4 yet the error is on line 5
    > How can we tell?????


    AFAIU it is a non-printing character..
    <http://www.physci.org/codes/char.jsp?char=0>

    --
    Andrew Thompson
    http://www.PhySci.org/ Open-source software suite
    http://www.PhySci.org/codes/ Web & IT Help
    http://www.1point1C.org/ Science & Technology
    Andrew Thompson, Jun 1, 2004
    #3
  4. NB

    Chris Uppal Guest

    NB wrote:
    > while ((byteCount = zipInputStream.read(buffer, 0, BUFFER_SIZE))
    > != -1) {
    > byteArrayOutputStream.write(buffer, 0, BUFFER_SIZE);
    > }


    I don't know if it's the cause of your problem, but InputStream.read(byte[],
    int, int) is /not/ guaranteed to read as many bytes as you asked for, it may
    fall short if that's the way that the underlying connection works. E.g. it
    might read no more than 17 bytes in any one gulp.

    IIRC, ZipInputStream does in fact behave like this -- the amount it returns in
    one gulp is somehow sensitive to the buffering between it and zlib.

    -- chris
    Chris Uppal, Jun 1, 2004
    #4
  5. NB

    NB Guest

    In article <lm4vc.32044$eY2.23558@attbi_s02>, says...

    > You only show lines 1-4 yet the error is on line 5
    > How can we tell?????


    I was wondering about the same thing but that's error I get.
    NB, Jun 1, 2004
    #5
  6. NB

    NB Guest

    In article <>,
    says...
    > I'm trying to read the following (very simple) zipped XML document:


    Solved:

    Document document = saxBuilder.build(new StringReader
    (byteArrayInputStream.toString().trim()));
    NB, Jun 2, 2004
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Mark Kelly

    Streaming ZIPPED files

    Mark Kelly, May 11, 2004, in forum: ASP .Net
    Replies:
    6
    Views:
    527
    Mark Kelly
    May 12, 2004
  2. Amelyan

    Download zipped files

    Amelyan, Mar 7, 2006, in forum: ASP .Net
    Replies:
    0
    Views:
    393
    Amelyan
    Mar 7, 2006
  3. Tony Prichard
    Replies:
    0
    Views:
    718
    Tony Prichard
    Dec 12, 2003
  4. =?Utf-8?B?Y2FzaGRlc2ttYWM=?=

    Zipped folder problem

    =?Utf-8?B?Y2FzaGRlc2ttYWM=?=, Jul 17, 2006, in forum: ASP .Net
    Replies:
    0
    Views:
    365
    =?Utf-8?B?Y2FzaGRlc2ttYWM=?=
    Jul 17, 2006
  5. mir nazim
    Replies:
    0
    Views:
    265
    mir nazim
    Dec 19, 2003
Loading...

Share This Page