Problem reading zipped XML document

N

NB

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

Chris Uppal

NB said:
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
 
N

NB

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

Solved:

Document document = saxBuilder.build(new StringReader
(byteArrayInputStream.toString().trim()));
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,015
Latest member
AmbrosePal

Latest Threads

Top