Problem beim XML-Lesen

Status
Nicht offen für weitere Antworten.
J

Jboss

Gast
Hi,
ich hab ein Problem mit dem Lesen einer XML-Datei.
Der erster Block von Product wird ohne Problem gelesen danach kriege ich Exceptions dass das Rootelement wohlgeformt sein muss .Ist das Document nicht wohlgeformt ?
Wie kann man Rootelemt einer XML-Datei einfügen ?

javax.xml.stream.XMLStreamException: ParseError at [row,col]:[9,2]
Message: The markup in the document following the root element must be well-formed.
at com.sun.org.apache.xerces.internal.impl.XMLStreamReaderImpl.next(Unknown Source)
at com.sun.xml.internal.stream.XMLEventReaderImpl.nextEvent(Unknown Source)

Code:
<?xml version="1.0" encoding="UTF-8"?>
<product>
	<id>001</id>
	<name>monitor</name>	
</product>
<product>
	<id>002</id>
	<name>laptop</name>	
</product>

Code:
public void readData() {
	try {
	    // First create a new XMLInputFactory
	    XMLInputFactory inputFactory = XMLInputFactory.newInstance();
	    // Setup a new eventReader
	    InputStream in = new FileInputStream(file);
	    XMLEventReader eventReader = inputFactory.createXMLEventReader(in);
	    // Read the XML document
	    while (eventReader.hasNext()) {
		XMLEvent event = eventReader.nextEvent();
		if (event.isStartElement()) {
		if (event.asStartElement().getName().getLocalPart() == ("id")) {
			event = eventReader.nextEvent();			
			System.out.println(event.asCharacters().getData());
			continue;
		    }		
		    if (event.asStartElement().getName().getLocalPart() == ("name")) {
			event = eventReader.nextEvent();
			spieler = event.asCharacters().getData();
			System.out.println(event.asCharacters().getData());
			continue;
		    }
		    	    
		}
	    }
	} catch (FileNotFoundException e) {
	    e.printStackTrace();
	} catch (XMLStreamException e) {
	    e.printStackTrace();
	}
 }
 

mvitz

Top Contributor
Ohne jetzt wahnsinnig viel Ahnung von XML zu haben, glaube ich, dass du genau 1 Root Element haben musst.
Versuch es mal so:
Code:
<?xml version="1.0" encoding="UTF-8"?>
<products>
  <product>
    <id>001</id>
    <name>monitor</name>   
  </product>
  <product>
    <id>002</id>
    <name>laptop</name>   
  </product>
</products>
 
Status
Nicht offen für weitere Antworten.
Ähnliche Java Themen
  Titel Forum Antworten Datum
N jdom problem beim lesen von child elementen XML & JSON 5
M Read / write Problem beim ByteStrom XML & JSON 2
whitenexx Problem beim parsen von Facebook XML XML & JSON 3
G Problem beim schreiben von XML in eine File XML & JSON 2
B Problem beim löschen von ChildNodes aus einem XML-DOM XML & JSON 3
E JDOM - Problem beim Zusammenfügen zweier Dateien XML & JSON 2
T Problem beim Parsen von Attribut xmlns="urn:com:test&qu XML & JSON 6
G DOCTYPE Problem beim Transformer/TransformerFactory etc. XML & JSON 13
P Problem beim erstellen eines neuen Elements (JDOM) XML & JSON 5
S Problem beim Erstellen eines pdfs XML & JSON 3
R Problem beim Auslesen von Attributen XML & JSON 4
O XPATH Problem - Anfänger XML & JSON 4
so_ein_Komischer Problem mit Clean and Build XML & JSON 1
P JAXB-Problem XML & JSON 1
W Problem mit dem mit XML sortierung XML & JSON 2
S Jaxb Unmarshalling Problem XML & JSON 4
E einfaches Problem XML + XSD + jedit XML & JSON 2
S Xslt Problem XML & JSON 2
B JasperReport Problem in Runtime XML & JSON 2
S Problem with JAXB unmarshalling classes that have the same name in @XmlRootElement XML & JSON 2
R Problem bei: XML und XSL zu HTML XML & JSON 2
M XML write Problem zweiter Ansatz XML & JSON 3
M XML read Problem XML & JSON 4
M XML write Problem XML & JSON 2
M XML Unicode Problem XML & JSON 2
S XJC --> Java-Objects compile Problem XML & JSON 4
F XPath-Problem mit DOM4J XML & JSON 8
B JPA + JAXB Mapping Problem XML & JSON 2
T XPath Problem: finden einer Node nach Attributswert XML & JSON 2
S Encoding Problem XML & JSON 7
K JAXB und Maps -> Marshalling-Problem XML & JSON 6
M JExcelAPI (JXL) Encoding Problem XML & JSON 11
S DOM Parsen Problem mit HTML Sonderzeichen XML & JSON 4
A aus xml --> html Problem XML & JSON 3
Y stax Problem XML & JSON 3
slawaweis Problem mit XSLT (wahrscheinlich ein Bug in Java 6) XML & JSON 16
P XPath Problem XML & JSON 2
M Problem mit FOP in Java Programm XML & JSON 2
S Problem mit XPath XML & JSON 4
J Problem mit compile einer XSD XML & JSON 3
N problem bei xml lesen mit jdom XML & JSON 2
A XPath Problem XML & JSON 2
W JDOM element ändern funzt nich :( [problem gelöst] XML & JSON 3
G Problem mit XML-Schema Validierung mit Java XML & JSON 12
B jdom: getChildren() problem XML & JSON 4
H XSL-FO Problem mit If XML & JSON 2
loadbrain XPath Problem XML & JSON 2
T addContent / Problem mit Variable XML & JSON 2
F Problem mit JAXB Unmarshaller XML & JSON 2
F JDOM und XPath - Problem mit Namespace ohne Prefix XML & JSON 5
8 SAXParser Problem, startElement wird nicht ausgeführt XML & JSON 2
M Java und XSLT: Performanz-Problem XML & JSON 5
X JDOM SAXBuilder Validationschema - Problem XML & JSON 8
G Problem mit getContent XML & JSON 4
K stax problem XML & JSON 2
S Problem mit SAX XML & JSON 6
A Problem mit JasperReport XML & JSON 6
C XSD Problem XML & JSON 16
R Problem bei Erstellung von XML(JDOM) XML & JSON 3
R Problem mit SAX-Parser characters() XML & JSON 7
M XPath Problem im Zusammenhang mit document() XML & JSON 2
Z Problem mit getNodeValue() und setNodeValue() in DOM XML & JSON 6
H JAXB CUSTOMIZATION PROBLEM XML & JSON 2
M XPATH und RSS (Problem namespaces) XML & JSON 7
P SAXParser problem? XML & JSON 2
V Problem mit xsd XML & JSON 2
P XML mit hilfe von JDOM abspeichern macht Problem XML & JSON 6
G Problem mit addContent() XML & JSON 4
B DTD Problem - Reihenfolge der Einträge XML & JSON 2
K Problem mit ant/java web services XML & JSON 4
K xml Datei mit JDOM erzeugen, Problem Namespaces XML & JSON 1
P Problem mit XML und DOM XML & JSON 2
W Ungleiches Escaping beim Marshalling XML & JSON 8
S Hilfe beim RDF-Graphen XML & JSON 0
R Beim Serialisieren fehlt die letzte Zeile XML & JSON 5
N Probleme bei der Formatierung beim Einfügen und löschen über DOM XML & JSON 7
A Leeres Array beim Auslesen von XML-Datei XML & JSON 4
L Jackson JSON: Probleme beim einlesen XML & JSON 1
A XML-Fehler beim Prefix von xmlns XML & JSON 2
K Beim Parsen einer XML-Datei Connection timed out XML & JSON 4
G Langsam beim SAX-Parsen - woran liegts? XML & JSON 2
A Fehler beim Erzeugen eines XML-Schema XML & JSON 4
B Validierung nur beim einlesen oder auch beim schreiben? XML & JSON 4
D JAXBException beim Marshaller XML & JSON 4
J Dateinamen beim Start auslesen XML & JSON 8
F Hilfe beim bearbeiten von XML elemente XML & JSON 3
N XStream ConversionException beim Deserialisieren in (Hibernate)Objekt XML & JSON 6
hdi Probleme beim Erstellen einer XML XML & JSON 7
F Probleme beim html parsen mit tagsoup XML & JSON 4
M Probleme beim Parsen eines gefilterten XML-Dokuments XML & JSON 6
L Reihenfolge beim xml Datei parsen einhalten? XML & JSON 8
H JAXB Probleme beim Unmarshalling XML & JSON 3
sylo Beim Erzeugen einer XML Datei auch die XML Struktur erzeugen? XML & JSON 11
N Hilfe beim Einstieg in EMF XML & JSON 6
D Das Programm hängt etwa 5 Sekunden beim Aufruf der parse-Methode XML & JSON 6
D Tabs/Einrückungen der XML-Elemente gehen beim Schreiben verloren XML & JSON 5
G XML Tag beim Einlesen manipulieren XML & JSON 2
S Probleme beim erstellen einer Jar XML & JSON 12
P NullPointerException beim Auslesen XML & JSON 8
M Performance beim Binding XML & JSON 2

Ähnliche Java Themen

Neue Themen


Oben