xml


Adding XML element with Scala's XML Support


I got a pretty basic question but don't seem to find the elegant Scala way of doing this.
I want to insert programatically generated XML tags into an existing XML structure that I read in from a file using
xml.XML.loadFile(...)
In How do I create an XML root node in Scala without a literal element name? I found this approach for creating my tags.
def textElem(name: String, text: String) = Elem(null, name, Null, TopScope, Text(text))
Having the XML tree
<root>
<set start="christmas">
<data>
<val>ABC</val>
...
</data>
<custom>
<entry>DEF</entry>
<!-- APPEND HERE -->
</custom>
</set>
<set start="halloween">
...
</set>
</root>
How do I select the custom section from the christmas set, append my programatically generated XML tags and save the whole XML tree back to a file?
Thanks for your Help!
Q. How do I select the custom section from the christmas set ?
A. In Scala, we can use def \\(that: String): NodeSeq
val custom = christmas \\ "custom"
println("-- print custom element --")
println(custom)
println("-- end --")
output:
-- print custom element --
<custom>
<entry>DEF</entry>
<!-- APPEND HERE -->
</custom>
-- end --
Q. append my programatically generated XML tags
A. It would be useful => scala - XML insert/update
And this link => substituting xml values programatically with scala
For example, I wrote like this:
It looks like, transform method will make it easy to manipulate XMLs.
// Q. Append my programatically generated XML tags
// A. Use RewriteRule#transform
val rule = new AppendRule
val appended = rule.transform(christmas)
println("-- print custom element --")
println(pp.format(appended(0)))
println("-- end --")
}
}
class AppendRule extends RewriteRule {
override def transform(n: Node): Seq[Node] = n match {
case <entry>{v}</entry>
=> println(s"Find value in tag <entry> => $v")
<entry>ABC</entry>
<entry>{v}</entry>
case elem: Elem
=> elem copy (child = elem.child flatMap (this transform))
case other
=> other
}
}
output:
Find value in tag <entry> => DEF
-- print custom element --
<root>
<set start="christmas">
<data>
<val>ABC</val>
</data>
<custom>
<entry>ABC</entry>
<entry>DEF</entry>
<!-- APPEND HERE -->
</custom>
</set>
<set start="halloween"> </set>
</root>
-- end --
Q. save the whole XML tree back to a file
A. Please use scala.xml.XML#save
Whole source code:
[Wandbox]三へ( へ՞ਊ ՞)へ ハッハッ

Related Links

Symfony Twig and xml - Document is empty error
How can I add name of parent node to first child xsl?
Using contains function with xsl:key
Change output based on value in xsl:key in XSLT 1.0
Evaluating list of XPATH expressions stored in a set, in scala
Inserting XML data to a XML file
select several consecutive XML tags in vim
How to deserialize same XML Element with different namespace into different elements in a struct
Playgrounds crashing with “unknown error” when using XML-parsing Swift libraries
Delphi add child element to XPath result
how to find Xpath for the below xml with namespace for attribute <dto:telefono>
How to make two attributes in XML to be unique (DTD)
Delphi SOAP empty array (mapping to objects)
Set Folder, Subfolder and File Permission Set-ACL Powershell Script. Setting File Inherititance does not work
Getting error in xsl file
Call from an xsl file to another and receiving a response

Categories

HOME
url-redirection
google-app-engine
assembly
google-cloud-platform
codenvy
telegram
nvd3.js
datastore
package
owl-carousel
nuget
seo
soa
simulink
python-multithreading
java.util.logging
mxgraph
ycsb
comsol
heat
atlassian-stash
job-scheduling
chargify
google-content-api
easeljs
game-theory
fat
corpus
flooding
ms-access-web-app
mat
android-maps
datastax-enterprise-graph
bringtofront
uitapgesturerecognizer
panoramas
mustache
dumpbin
autolisp
sharding
slacktextviewcontroller
swagger-php
timing
recurrent-neural-network
c9.io
hackintosh
bindingnavigator
tcc
uiimage
double-click
flask-restful
linter
wcftestclient
twilio-click-to-call
magic-draw
custom-font
modelsim
android-testing
skin
bbpress
visual-studio-2008-sp1
atlas
custom-lists
mta
beamer
x-tag
jclouds
android-wake-lock
query-by-example
kiosk
ikiwiki
uiactionsheet
beanstalk
pushbots
false-sharing
highland.js
dynatree
tfs-workitem
text-search
iced-coffeescript
wso2greg
sha512
google-refine
teamcity-9.1
git-ftp
fail2ban
kubuntu
j-security-check
umlgraph
openbabel
adehabitathr
theos
ghcjs
post-commit-hook
formview
universal-image-loader
jama
encryption-asymmetric
mdichild
terminate
accesscontrolexception
mysql-error-1044
away3d
isqlquery
ice-cube
inappsettingskit
jquery-1.9
diem
device-width
onmousemove
mkv
boost-msm
cuda-gdb
floating-point-exceptions
jquery-address
quotation-marks
msr
message-passing
glui
tui
lemmatization

Resources

Encrypt Message