java sax parser - ændre xml - dokument

, demo eksempel. her er det input, xml - fil, vi har brug for at ændre ved at tilføje < resultatet er > passere < resultat />,,, ved afslutningen på < /marks > tag,,, <?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> , ,,SAXModifyDemo.java,, package com.tutorialspoint.xml; import java. io. *; import org. xml. sax. *; import javax. xml. parsers. *; import org.xml.sax.helpers.defaulthandler; offentlige klasse saxmodifydemo udvider defaulthandler (statiske snor displaytext [] = ny serie [1000]; statiske int numberlines = 0; statiske snor fordybning = "," offentlige statisk. vigtigste (string - args []) (prøve (sag inputfile = ny fil ("input. txt"); saxparserfactory fabrik = saxparserfactory. newinstance(); saxmodifydemo mål = nye saxmodifydemo(); mål nr. childloop (inputfile); filewriter filewriter = nye filewriter ("newfile. xml -") til (int loopindex = 0, loopindex < numberlines; loopindex + +) (filewriter. skrive (displaytext [loopindex]. tochararray()); filewriter. skrive ('), n') system. println (displaytext [loopindex]. tostring())} filewriter. close();} fangst (undtagen e) (e.printstacktrace (system. øhh)}} offentlige tomrum childloop (file input) (defaulthandler kontaktperson = det, saxparserfactory fabrik = saxparserfactory. newinstance(); prøve (saxparser saxparser = fabrik. newsaxparser(); saxparser. analysere (input, kontakt)} fangst (throwable t) {}} offentlige tomrum startdocument() {displaytext [numberlines] = aftryk. displaytext [numberlines] + = "<?xml - version =. "1." koder =. "" + "utf - 8" + "og"?> ". numberlines + +) offentlige tomrum processinginstruction (serie mål, snor data) (displaytext [numberlines] = aftryk. displaytext [numberlines] + =" <. "; displaytext [numberlines] + = mål; hvis (data.= null &&data. length() > 0) (displaytext [numberlines] + = "; displaytext [numberlines] + = data;} displaytext [numberlines] + ="?> ". numberlines + +) offentlige tomrum startelement (string - uri, snor localname, snor qualifiedname, attributter attributter) (displaytext [numberlines] = fordybning; fordybning + =". "displaytext [numberlines] + = < displaytext [numberlines] + = qualifiedname; hvis (attributter.= null) (int numberattributes = attributter. getlength(); (int loopindex = 0, loopindex < numberattributes; loopindex + +) (displaytext [numberlines] + = "; displaytext [numberlines] + = attributter. getqname (loopindex); displaytext [numberlines] + = =." "displaytext [numberlines] + = attributter. getvalue (loopindex); displaytext [numberlines] + = '«) displaytext [numberlines] + = > «. numberlines + +) offentlige tomrum tegn (char tegn [], int start, int længde) {snor characterdata = (nye serie (tegn start, længde). trim(); hvis (characterdata. indexof ("), n ") < 0 &&characterdata. length()> 0) (displaytext [numberlines] = aftryk. displaytext [numberlines] + = characterdata; numberlines + +) offentlige tomrum endelement (string - uri, snor localname, snor qualifiedname) {fordybning = aftryk. substring (0, aftryk. length() - 4); displaytext [numberlines] = displaytext aftryk. [numberlines] + = "< /" displaytext [numberlines] + = qualifiedname; displaytext [numberlines] + = > «. numberlines +; hvis (qualifiedname. = ("mærker") (startelement ("," resultat "," resultat ", null); bogstaver (" pass ". tochararray(), 0," gå ". length()); endelement (", "resultat", "betyder");}}}, ville dettegiver følgende resultat:, <?xml - version = "0" kodning = "utf - 8"?> < klasse > < studerende rollno = 393 "> < firstname > dinkar < /firstname > < lastname > kad < /lastname > < øgenavn > dinkar <. /øgenavn > < marks > 85 < /marks > < resultatet er > passere < /resultat > < /student > < studerende rollno =" 493 "> < firstname > vaneet < /firstname > < lastname > gupta < /lastname > < øgenavn > vinni < /øgenavn > < marks > 95 < /marks > < resultat > og gå< /resultat > < /student > < studerende rollno = "593" > < firstname > jasvir < /firstname > < lastname > singn < /lastname > < øgenavn. > jazz < /øgenavn > < marks > 90 < /marks > < resultatet er > passere < /resultat > < /student > < /klasse >,



Previous:
Next Page: