ruby on rails - How to replace tag with some value in XML using Nokogiri -
i have predefine xml template tags need replaced. tag values come dynamically front-end.
<?xml version="1.0"?> <catalog> <book id="bk101"> <author>author1</author> <title>title1</title> <genre>computer</genre> <price>44.95</price> <publish_date>2000-10-01</publish_date> <description>an in-depth @ creating applications xml.</description> </book> <book id="bk102"> <author>author2</author> <title>title2</title> <genre>fantasy</genre> <price>5.95</price> <publish_date>2000-12-16</publish_date> <description>a former architect battles corporate zombies, evil sorceress, , own childhood become queen of world.</description> </book> </catalog>
in above example need replace title1
, title2
, author1
, author2
actual value dynamically.
what best way this? using nokogiri in ruby code have had no luck.
the basic idea need search xml <book>
tags. each book found, retrieve block of values apply it. find <author>
tag , replace text. find <title>
tag, , replace text also. go next book.
however, in example, writing code overkill when simple gsub
in 1 pass:
xml = '<?xml version="1.0"?> <catalog> <book id="bk101"> <author>author1</author> <title>title1</title> <genre>computer</genre> <price>44.95</price> <publish_date>2000-10-01</publish_date> <description>an in-depth @ creating applications xml.</description> </book> <book id="bk102"> <author>author2</author> <title>title2</title> <genre>fantasy</genre> <price>5.95</price> <publish_date>2000-12-16</publish_date> <description>a former architect battles corporate zombies, evil sorceress, , own childhood become queen of world.</description> </book> </catalog> ' values = { 'title1' => 'moby dick', 'author1' => 'herman melville', 'title2' => 'tom sawyer', 'author2' => 'mark twain', } puts xml.gsub(regexp.union(values.keys), values) # >> <?xml version="1.0"?> # >> <catalog> # >> <book id="bk101"> # >> <author>herman melville</author> # >> <title>moby dick</title> # >> <genre>computer</genre> # >> <price>44.95</price> # >> <publish_date>2000-10-01</publish_date> # >> <description>an in-depth @ creating applications # >> xml.</description> # >> </book> # >> <book id="bk102"> # >> <author>mark twain</author> # >> <title>tom sawyer</title> # >> <genre>fantasy</genre> # >> <price>5.95</price> # >> <publish_date>2000-12-16</publish_date> # >> <description>a former architect battles corporate zombies, # >> evil sorceress, , own childhood become queen # >> of world.</description> # >> </book> # >> </catalog>
this use of gsub
isn't used often, i've used many times when substituting values templates. using tags/keys guaranteed unique in document essential, flag them using leading , trailing double underscores. in other words __title1__
, __author1__
, etc.
doing can replace content of other fields, such <genre>
, <price>
, etc.
name variables in form same keys/tags, , task becomes easier because should receive hash of field-names , field-values, becomes source hash used in gsub
.
be sure verify/sanitize values before substituting. users mistype , malicious ones can deliberately enter data in attempt break code, or worse, whatever xml fed into.
Comments
Post a Comment