Ich versuche schon recht lange ein xml Dokument zu parsen in dem das End-Tag fehlt (Weshalb fehlt es? -> es wird zur Laufzeit abgegriffen). Leider schlägt das immer fehl.
Gibt es eine Möglichkeit dies zu tun?
[Java]
<?xml version="1.0" encoding="ISO-8859-1"?>
<!-- Edited by XMLSpy® -->
<breakfast_menu>
<food>
<name>Strawberry Belgian Waffles</name>
<price>$7.95</price>
<description>light Belgian waffles covered with strawberries and whipped cream</description>
<calories>900</calories>
</food>
<food>
<name>Berry-Berry Belgian Waffles</name>
<price>$8.95</price>
<description>light Belgian waffles covered with an assortment of fresh berries and whipped cream</description>
<calories>900</calories>
</food>
<---------------------------------------------------------------hier fehlt das End-Tag
[/code]
Gibt es eine Möglichkeit dies zu tun?
[Java]
<?xml version="1.0" encoding="ISO-8859-1"?>
<!-- Edited by XMLSpy® -->
<breakfast_menu>
<food>
<name>Strawberry Belgian Waffles</name>
<price>$7.95</price>
<description>light Belgian waffles covered with strawberries and whipped cream</description>
<calories>900</calories>
</food>
<food>
<name>Berry-Berry Belgian Waffles</name>
<price>$8.95</price>
<description>light Belgian waffles covered with an assortment of fresh berries and whipped cream</description>
<calories>900</calories>
</food>
<---------------------------------------------------------------hier fehlt das End-Tag
[/code]
Java:
import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.w3c.dom.Document;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;
public class TestEventStaX {
private static void parseXmlFile(){
try{
File file = new File("/tmp/TEST.xml");
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
dbf.setValidating(false);
DocumentBuilder db = dbf.newDocumentBuilder();
// FileInputStream fstream = new FileInputStream("/tmp/TEST.xml");
// DataInputStream in = new DataInputStream(fstream);
// Document doc = db.parse(in);
Document doc = db.parse(file);
doc.getDocumentElement().normalize();
System.out.println("Root element " + doc.getDocumentElement().getNodeName());
NodeList nodeLst = doc.getElementsByTagName("sample");
System.out.println("Information of all samples");
}catch(ParserConfigurationException pce) {
pce.printStackTrace();
}catch(SAXException se) {
se.printStackTrace();
}catch(IOException ioe) {
ioe.printStackTrace();
}
}
public static void main(String args[]) {
parseXmlFile();
}
}
Java:
[Fatal Error] TEST.xml:10:1: XML document structures must start and end within the same entity.
org.xml.sax.SAXParseException: XML document structures must start and end within the same entity.
at com.sun.org.apache.xerces.internal.parsers.DOMParser.parse(DOMParser.java:249)
at com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderImpl.parse(DocumentBuilderImpl.java:284)
at javax.xml.parsers.DocumentBuilder.parse(DocumentBuilder.java:208)
at CSV_Reader_Mail.TestEventStaX.parseXmlFile(TestEventStaX.java:30)
at CSV_Reader_Mail.TestEventStaX.main(TestEventStaX.java:47)