java - Problem parsing UTF-8 XML File with SAX Parser -
i trying parse xml file made , pull information out of it. basing off of this tutorial. parsing connecting xml file ok , evaluates first tag crash afterwards seen in output. attached below xml file layout, java parsing code, , console output.
any idea why not reading next tag of xml, , if so, should change?
xml file:
<?xml version='1.0' encoding='utf-8' standalone='yes' ?> <filepath>/mnt/sdcard/audio_recorder/anonymous22242.3gp</filepath> <filename>anonymous22242.3gp</filename> <annotation> <file>anonymous22242.3gp</file> <timestamp>0:06</timestamp> <note>test1</note> </annotation> <annotation> <file>anonymous22242.3gp</file> <timestamp>0:09</timestamp> <note>lol</note> </annotation> <annotation> <file>anonymous22242.3gp</file> <timestamp>0:09</timestamp> <note>lolol</note> </annotation>
java file:
private static string filedirectory; private final static arraylist<string> allfilenames = new arraylist<string>(); private final static arraylist<string[]> allannotations = new arraylist<string[]>(); private static string[] currentannotation = new string[3]; public static void main(string[] args) { // todo auto-generated method stub try { saxparserfactory factory = saxparserfactory.newinstance(); saxparser playbackparser = factory.newsaxparser(); defaulthandler handler = new defaulthandler() { boolean audiofullpath = false; boolean audioname = false; boolean annotationfile = false; boolean annotationtimestamp = false; boolean annotationnote = false; public void startelement(string uri, string localname, string qname, attributes attributes) throws saxexception { system.out.println("start element :" + qname); if (qname.equalsignorecase("filepath")) { audiofullpath = true; } if (qname.equalsignorecase("filename")) { audioname = true; } if (qname.equalsignorecase("file")) { annotationfile = true; } if (qname.equalsignorecase("timestamp")) { annotationtimestamp = true; } if (qname.equalsignorecase("note")) { annotationnote = true; } } public void endelement(string uri, string localname, string qname) throws saxexception { system.out.println("end element :" + qname); } public void characters(char ch[], int start, int length) throws saxexception { if (audiofullpath) { string filepath = new string(ch, start, length); system.out.println("full path : " + filepath); filedirectory = filepath; audiofullpath = false; } if (audioname) { string filename = new string(ch, start, length); system.out.println("file name : " + filename); allfilenames.add(filename); audioname = false; } if (annotationfile) { string filename = new string(ch, start, length); currentannotation[0] = filename; annotationfile = false; } if (annotationtimestamp) { string timestamp = new string(ch, start, length); currentannotation[1] = timestamp; annotationtimestamp = false; } if (annotationnote) { string note = new string(ch, start, length); currentannotation[2] = note; annotationnote = false; allannotations.add(currentannotation); } } }; file file = new file( "c:\\documents , settings\\anonymous.xml"); inputstream inputstream = new fileinputstream(file); reader xmlreader = new inputstreamreader(inputstream, "utf-8"); inputsource xmlsource = new inputsource(xmlreader); xmlsource.setencoding("utf-8"); playbackparser.parse(xmlsource, handler); system.out.println(filedirectory); system.out.println(allfilenames); system.out.println(allannotations); } catch (exception e) { e.printstacktrace(); } } }
output:
start element :filepath full path : /mnt/sdcard/audio_recorder/anonymous22242.3gp end element :filepath org.xml.sax.saxparseexception: markup in document following root element must well-formed. @ com.sun.org.apache.xerces.internal.util.errorhandlerwrapper.createsaxparseexception(unknown source) @ com.sun.org.apache.xerces.internal.util.errorhandlerwrapper.fatalerror(unknown source) @ com.sun.org.apache.xerces.internal.impl.xmlerrorreporter.reporterror(unknown source) @ com.sun.org.apache.xerces.internal.impl.xmlscanner.reportfatalerror(unknown source) @ com.sun.org.apache.xerces.internal.impl.xmldocumentscannerimpl$trailingmiscdriver.next(unknown source) @ com.sun.org.apache.xerces.internal.impl.xmldocumentscannerimpl.next(unknown source) @ com.sun.org.apache.xerces.internal.impl.xmldocumentfragmentscannerimpl.scandocument(unknown source) @ com.sun.org.apache.xerces.internal.parsers.xml11configuration.parse(unknown source) @ com.sun.org.apache.xerces.internal.parsers.xml11configuration.parse(unknown source) @ com.sun.org.apache.xerces.internal.parsers.xmlparser.parse(unknown source) @ com.sun.org.apache.xerces.internal.parsers.abstractsaxparser.parse(unknown source) @ com.sun.org.apache.xerces.internal.jaxp.saxparserimpl$jaxpsaxparser.parse(unknown source) @ javax.xml.parsers.saxparser.parse(unknown source) @ main.main(main.java:131)
that's not xml. there must single root element containing of rest.
Comments
Post a Comment