java stax parser - ændre xml - dokument

, demo eksempel, her er xml - er vi nødt til at ændre:,,, <?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>singh</lastname> <nickname>jazz</nickname> <marks>90</marks> </student> , ,,Demo Example:,,,StAXModifyDemo.java,, package com.tutorialspoint.xml; import java.io.File; import java.io.filenotfoundexception; import java.io.filereader; import java.io.ioexception; import java.util.iterator; import java.util.list; 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.startelement; import javax.xml.stream.events.xmlevent; import org.jdom2.document; import org.jdom2.element; import org.jdom2.jdomexception; import org.jdom2.input.saxbuilder; import org.jdom2.output.format; import org.jdom2.output.xmloutputter; offentlige klasse staxmodifydemo {offentlige statisk tomrum vigtigste (snor -] args) (prøve (xmlinputfactory fabrik = xmlinputfactory. newinstance(); xmleventreader eventreader = fabrik. createxmleventreader (nye filereader ("input. txt"); saxbuilder saxbuilder = nye saxbuilder(); dokument = saxbuilder. bygge (nye fil ("input. txt"); element rootelement = dokument. getrootelement(); listen < element > studentelements = rootelement. getchildren ("studerende"), 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 = attributter. next(). getvalue(); hvis (rollno. equalsignorecase (393)) (//- elev med roll - nr. 393 for int (i = 0, - < studentelements. size(); jeg + +) (element studentelement = studentelements. kom (i); hvis (studentelement. getattribute ("rollno"). getvalue(). equalsignorecase (393)) (studentelement. removechild ("mærker"); studentelement. addcontent (nyt element ("mærker"). settext ("80")}}}} pause.}} xmloutputter xmloutput = nye xmloutputter(); //display xml - xmloutput. setformat (format. getprettyformat()); xmloutput. produktion (dokument system.);} fangst (filenotfoundexception e) (f. printstacktrace();} fangst (xmlstreamexception e) (f. eks. printstacktrace();} fangst (jdomexception e) (f. printstacktrace();} fangst (ioexception e) (f. printstacktrace();}}}, vil det føre til følgende resultat:, < studerende rollno = 393 "> < firstname > dinkar < /firstname > < lastname>kad</lastname> <nickname>dinkar</nickname> <marks>80</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>singh</lastname> <nickname>jazz</nickname> <marks>90</marks> </student> ,



Previous:
Next Page: