, demo eksempel, med henblik på at køre dette eksempel, du skulle have jdom.jar i din ansøgning er classpath.download jdom-2.0.5. zip.,, 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>Vineet</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> </class> , ,,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 (string [] args) (prøve (xmlinputfactory farbejde og børnepasning = 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 og jeg < 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. 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>Vineet</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> ,
apache xerces stax parser - ændre xml - dokument
Next Page:xstream tutor tutor