[Gambas-user] Edit XML

Caveat Gambas at ...1950...
Sat Nov 20 13:54:27 CET 2010


Hi Mauricio

Sorry for the late reply, I was doing a lot of thinking. :-)

Perhaps you might like something along these lines:

  DIM rewriter AS NEW XMLRewriter
  ' Read the original input XML (See http://pastebin.com/aR11N5uc)
  rewriter.parseInput(User.home &/ "Heroes.xml")
  ' All existing Text Entities in /all_characters/characters/heroe/name
with current value "Claire Bennet" will be updated to "Banana Pudding"
  rewriter.updateTextByPath("/all_characters/characters/heroe/name",
"Claire Bennet", "Banana Pudding")
  ' All existing Text Entities
in /all_characters/characters/villain/played_by will be updated to
"Someone evil" regardless of the current content

rewriter.updateTextByPath("/all_characters/characters/villain/played_by", NULL, "Someone evil")
  ' All Text Entities with the value "Being silly" get updated to "Being
a complete donut brain"
  rewriter.updateTextByValue("Being silly", "Being a complete donut
brain")
  ' Change the value of the attribute named "id" to "h1" on all Entities
having an Attribute  named "name" with a value of "Claire Bennet" 
  rewriter.changeAttribute("/all_characters/characters/heroe", "name",
"Claire Bennet", "id", "h1")
  ' Change the value of the attribute named "id" to "h2" on all Entities
having an Attribute  named "name" with a value of "Hiro Nakamura"
  rewriter.changeAttribute("/all_characters/characters/heroe", "name",
"Hiro Nakamura", "id", "h2")
  ' First correct a fault in the XML (Attribute called "name" has an
erroneous space at the front of the value)
  ' So this will change the value of the Attribute named "name" to
"Gabriel Sylar" on all Entities having an Attribute  named "name" with a
value of " Gabriel Sylar" (note the leading space)
  rewriter.changeAttribute("/all_characters/characters/villain", "name",
" Gabriel Sylar", "name", "Gabriel Sylar")
  ' Now continue updating the ids
  rewriter.changeAttribute("/all_characters/characters/villain", "name",
"Gabriel Sylar", "id", "v1")
  rewriter.changeAttribute("/all_characters/characters/heroe", "name",
"Graham", "id", "b1")
  rewriter.changeAttribute("/all_characters/characters/heroe", "name",
"Bill", "id", "b2")
  rewriter.changeAttribute("/all_characters/characters/heroe", "name",
"Tim", "id", "b3")
  ' Write out the new XML from the changed internal data model (See
http://pastebin.com/T0S96xiS)
  rewriter.writeXML(User.home &/ "NewHeroes.xml", TRUE, "UTF-8")

XMLRewriter is a class in a little project I've been working on.  The
idea of XMLRewriter is that it should be capable of parsing any XML
input and applying edits to it to create a new output XML.  The
XMLRewriter class itself does not need to know anything about the XML
it's being fed, as it automagically builds an internal data model of the
provided XML. :-D

Let me know if you're interested in taking this further, perhaps we can
go off-list to get into the details.

Regards,
Caveat

On Mon, 2010-11-15 at 22:58 -0600, Mauricio Baeza wrote:

> Hi all ...
> 
> With the library for XML I can, without problems ...
> 1 .- Create new XML file
> 2 .- Read any XML file
> 
> What I can not do is change just one attribute of an element of an
> existing file. Does anyone have a sample?
> 
> Greetings
> 
> 
> 





More information about the User mailing list