, demo eksempel, her er vi nødt til at analysere indgående xml - fil:,,, <?xml - version = "0"?> <class> <student rollno="393"> <firstname>dinkar</firstname> <lastname>kad</lastname> <nickname>dinkar</nickname> <marks>85</marks> </student> <student rollno="493"> <firstname>Vaneet</firstname> <lastname>Gupta</lastname> <nickname>vinni</nickname> <marks>95</marks> </student> <student rollno="593"> <firstname>jasvir</firstname> <lastname>singn</lastname> <nickname>jazz</nickname> <marks>90</marks> </student> </class> , ,,StAXParserDemo.java,, package com.tutorialspoint.xml; import java.io.filenotfoundexception; import java.io.filereader; import java.util.iterator; import javax.xml.stream.xmleventreader; import javax.xml.stream.xmlinputfactory; import javax.xml.stream.xmlstreamconstants; import javax.xml.stream.xmlstreamexception; import javax.xml.stream.events.attribute; import javax.xml.stream.events.characters; import javax.xml.stream.events.endelement; import javax.xml.stream.events.startelement; import javax.xml.stream.events.xmlevent; offentlige klasse staxquerydemo {offentlige statisk tomrum vigtigste (string [] args) {boolean bfirstname = falske boolean blastname = falske boolean - bnickname = falske boolean bmarks = falske boolean isrequestrollno = falske prøve (xmlinputfactory fabrik = xmlinputfactory. newinstance(); xmleventreader eventreader = fabrik. createxmleventreader (nye filereader ("input. txt"); snor requestedrollno = 393 ", mens (eventreader. hasnext()) (xmlevent begivenhed = eventreader. nextevent(); skift (ref. geteventtype()) (sag xmlstreamconstants.start_element: startelement startelement = begivenhed. asstartelement(); snor qname = startelement. getname(). getlocalpart(); hvis (qname. equalsignorecase (" studerende ") (iterator < attribut > attributter = startelement. getattributes(); snor rollno = attributes. next(). getvalue(); hvis (rollno. equalsignorecase (requestedrollno)) (system. println ("start element: studerende"); system. println ("roll - nr.:" + rollno); isrequestrollno = sandt.}} andre, hvis (qname. equalsignorecase ("firstname)) (bfirstname = sandt.} andre, hvis (qname. equalsignorecase (" lastname ") (blastname = sandt.} andre, hvis (qname. equalsignorecase (" øgenavn ") (bnickname = sandt.} andre, hvis (qname. equalsignorecase (" mærker ") (bmarks = sandt.} pause, sag xmlstreamconstants.characters: tegn tegn = begivenhed. ascharacters(); hvis (bfirstname &&isrequestrollno) (system. println ("fornavn:" + tegn. getdata()); bfirstname = falske} hvis (blastname &&isrequestrollno) (system. println ("sidste navn:" + tegn. getdata()); blastname = falske} hvis (bnickname &&isrequestrollno) (system. println ("nick navn:" + tegn. getdata());bnickname = falske} hvis (bmarks &&isrequestrollno) (system. println ("mærker" + tegn. getdata()); bmarks = falske} pause, sag xmlstreamconstants.end_element: endelement endelement = begivenhed. asendelement(); hvis (endelement. getname(). getlocalpart(). equalsignorecase ("studerende") &&isrequestrollno) {system. println ("sidste element: studerende"); system. println(); isrequestrollno = falske} pause.}}} fangst(filenotfoundexception e) (f. printstacktrace();} fangst (xmlstreamexception e) (f. printstacktrace();}}}, vil det føre til følgende resultat: begynd element: studerende rulle: 393 fornavn: dinkar efternavn: kad nick navn: dinkar varemærker: 85 ende element: studerende,
java stax parser til søgning i xml - dokument
Next Page:java stax parser - ændre xml - dokument