Home > Software engineering >  Easy to use XML parser
Easy to use XML parser

Time:01-03

I have XML as follows:

<Game>
   <ID>8585></ID>
   <ApplicationPath>F:\Games\gamename.zip</ApplicationPath>
   <DateAdded>2021-03-20T18:03:57.1370703-04:00</DateAdded>
   <DateModified>2021-04-16T22:00:45.3921555-04:00</DateModified>
   <Category>Platformer</Category>
   <Developer />
   <Notes />
</Game>
<Game>
   <ID>8855></ID>
   <ApplicationPath>F:\Games\hat\gamename.zip</ApplicationPath>
   <DateAdded>2021-03-20T18:03:57.1370703-04:00</DateAdded>
   <DateModified>2021-04-16T22:00:45.3921555-04:00</DateModified>
   <Category>Platformer</Category>
   <Developer />
   <Notes />
</Game>

I am looking for a very easy and simple way to parse through this XML, which contains thousands of lines such that I can search through it to find every instance of games where ApplicationPath contains "hat" and replace the associated Category of that game with "puzzle".

If this can be accomplished in Notepad , how? If not, I'm open to any recommendations.

Thank you

CodePudding user response:

With Saxon's Gizmo:

java net.sf.saxon.Gizmo -s:input.xml
/>update //Game[contains(ApplicationPath, 'hat')]/Category with "puzzle"
/>save output.xml
/>quit

CodePudding user response:

With xmlstarlet:

xmlstarlet edit -u '//Game[contains(ApplicationPath, "at")]/Category' \
  -v puzzle file.xml   

Not sure the quotes are working the same as Linux. If you have an error, invert the quotes:

  • single quotes -> double quotes
  • double quotes -> single quotes

Add -L to edit the file on the fly.

  • Related