I got XML items.xml file with (almost) the same values as my items table has, I mean fe. there is a field in the items table: level and for any id the level is set to 144, but in the XML file, the level= attribute is set to "1" (for the same id) – what is the best way to correct values like this?
It should go like this:
- Check value
levelin the database table for any id.- If the
levelvalue from the database is other than thelevel=""attribute for this ID, set it to the samelevelvalue as in the
database.
It can be kinda hard, since there is about ~40000 records to check.
I will appreciate some examples also!
Depending on what programming-language you are using, find the corresponding StAX-implementation. For Java I would go with
XMLStreamReader(JavaDocs) andXMLStreamWriter(JavaDocs). You should find some tutorials on the internet.When you encounter the
START_ELEMENTevent while reading the XML, check the tag’s name (getLocalName()). If you are on the correct tag, check for the attributes, i.e. using thegetAttribute...()-methods and handle the writing differently.Along all of this, use an
XMLStreamWriterto write your new XML to someOutputStream. After all, just write theOutputStreamto whereever you wish (File, etc.).Don’t forget to read your Input-XML using a
BufferedInputStream(or some other buffered way).Good luck!
P.S.: You can also use
XMLEventReaderorXMLEventWriter, but personally I preferXMLStreamReader/XMLStreamWriter. Also, you could use different StAX-Implementations likeWoodstox.P.P.S.: For PHP use XMLReader and XMLWriter. See here.