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