this question has answer here:
it's hometask. don't understand want me , why doesn't work. fails on reader.next() . path right. here's code on java of staxparser:
public class staxparser { private dances dances; private dance currentdance; private dancer currentdancer; public static void main(string[] args) { staxparser staxparser = new staxparser(); staxparser.parse("dancegroupconcerts.xml"); } public dances getdances() { return dances; } public staxparser() { } public void parse (string xmlfilename){ try { xmlstreamreader reader = xmlinputfactory.newinstance().createxmlstreamreader(new stringreader(xmlfilename)); int tag; string tagname; string text = new string(); while (reader.hasnext()){ tag = reader.next(); switch (tag){ case xmlstreamreader.start_element: tagname = reader.getlocalname(); if (tagname == "dances") dances = new dances(); if (tagname.equals("dance")) currentdance = new dance(); if (tagname.equals("dancer")) currentdancer = new dancer(); break; case xmlstreamreader.characters: text = reader.gettext(); break; case xmlstreamreader.end_element: tagname = reader.getlocalname(); if (tagname.equals("dancer")) currentdance.getdancers().add(currentdancer); if (tagname.equals("dance")) dances.getdance().add(currentdance); if (tagname.equals("type")) currentdance.settype(text); if (tagname.equals("scene")) currentdance.setscene(text); if (tagname.equals("numberofdancers")) currentdance.setnumberofdancers(numberofdancers.fromvalue(text)); if (tagname.equals("music")) currentdance.setmusic(music.fromvalue(text)); if (tagname.equals("name")) currentdancer.setname(text); if (tagname.equals("danceteam")) currentdancer.setnameofdanceteam(text); if (tagname.equals("age")) currentdancer.setage(biginteger.valueof(integer.valueof(text))); if (tagname.equals("experience")) currentdancer.setexperience(biginteger.valueof(integer.valueof(text))); if (tagname.equals("number")) currentdance.setnumber(biginteger.valueof(integer.valueof(text))); break; } } } catch (xmlstreamexception e) { e.printstacktrace(); } }
}
and xsd:
<?xml version="1.0" encoding="utf-8"?> <schema xmlns="http://www.w3.org/2001/xmlschema" xmlns:dance="dancers" targetnamespace="dancers"> <simpletype name="type"> <restriction base="string"></restriction> </simpletype> <simpletype name="scene"> <restriction base="string"></restriction> </simpletype> <simpletype name="numberofdancers"> <restriction base="string"> <enumeration value="mass"></enumeration> <enumeration value="solo"></enumeration> <enumeration value="pair"></enumeration> </restriction> </simpletype> <simpletype name="music"> <restriction base="string"> <enumeration value="phonogram"></enumeration> <enumeration value="alive"></enumeration> </restriction> </simpletype> <simpletype name="number"> <restriction base="integer"></restriction> </simpletype> <simpletype name="name"> <restriction base="string"></restriction> </simpletype> <simpletype name="danceteam"> <restriction base="string"></restriction> </simpletype> <simpletype name="age"> <restriction base="integer"></restriction> </simpletype> <simpletype name="experience"> <restriction base="integer"></restriction> </simpletype> <complextype name="dancer"> <sequence> <choice> <element name="name" type="dance:name"></element> <element name="nameofdanceteam" type="dance:danceteam"></element> </choice> <element name="age" type="dance:age"></element> <element name="experience" type="dance:experience"></element> </sequence> </complextype> <complextype name = "dance"> <sequence> <element name="type" type="dance:type"></element> <element name="scene" type="dance:scene"></element> <element name="numberofdancers" type="dance:numberofdancers"></element> <element name="music" type="dance:music"></element> <element name="dancers" type="dance:dancer" minoccurs="1" maxoccurs="unbounded"></element> <element name="number" type="dance:number"></element> </sequence> </complextype> <element name="dances"> <complextype> <sequence> <element name="dance" type="dance:dance" minoccurs="1" maxoccurs="unbounded"></element> </sequence> </complextype> </element> </schema>
and xml:
<?xml version="1.0" encoding="utf-8"?> <dances> <dance> <type>folk</type> <scene>dr. frank n. furter major</scene> <numberofdancers>mass</numberofdancers> <music>phonogram</music> <dancer> <name>majenta</name> <age>25</age> <experience>25</experience> </dancer> <dancer> <name>riff raff</name> <age>2500</age> <experience>25</experience> </dancer> <dancer> <name>columbia</name> <age>25</age> <experience>25</experience> </dancer> <dancer> <danceteam>guests</danceteam> <age>2566</age> <experience>667</experience> </dancer> <number>1</number> </dance> </dances>
thank's help. sorry if english bad.
the problem lies xml-file. there (invisible) content before <?xml...
causes parser fail. see question possible solution: content not allowed in prolog saxparserexception
to answer edit: possible won't see visible character in notepad++ - check following url possible solution: http://www.rgagnon.com/javadetails/java-handle-utf8-file-with-bom.html
Comments
Post a Comment