retrieving XML text

99 views
Skip to first unread message

Lucius Meredith

unread,
Jan 26, 2013, 10:14:26 AM1/26/13
to scale...@googlegroups.com
Dear ScalesXMLers,

After navigating to a particular point in an element how does one retrieve the XML text. For example, suppose we have

<myContainer><sound><signification>A</signification></sound><fury><nothingness>B</nothingness></fury></myContainer>

for( Right( p ) <- ScalesXPath("//myContainer/*[1]").withNameConversion(ScalesXPath.localOnly).evaluate(top(doc3)) ) { println( elem( p ).name ) }

will print {}sound

how do i get the who element text: <sound><signification>A</signification></sound>?

Best wishes,

--greg

Chris Twiner

unread,
Jan 26, 2013, 12:52:22 PM1/26/13
to scale...@googlegroups.com
Hiya,

import scales.utils.top
import scales.xml._
import ScalesXml._

import Functions._ // not needed for future 0.5

val xmls = "<myContainer><sound><signification>A</signification></sound><fury><nothingness>B</nothingness></fury></myContainer>"
val doc = loadXml(new java.io.StringReader(xmls))

val xpath = top(doc.rootElem).\*("sound"l) // we are already at
myContainer, tempted to implicit top as well for 0.5
println(text(xpath)) // works the same for any instance in TextImplicits

see http://scala-scales.googlecode.com/svn/sites/scales/scales-xml_2.9.2/0.4.4/XPathFunctions.html
for details (value and text are identical)

For the string xpaths you can either get the path out (as you have)
and call text on it or use the "string(....)" XPath 1.0 function to
extract it directly as per
http://scala-scales.googlecode.com/svn/sites/scales/scales-xml_2.9.2/0.4.4/StringXPaths.html
and get[String](path).

Hope that helps,

cheers,
Chris

Lucius Meredith

unread,
Jan 28, 2013, 3:36:31 PM1/28/13
to scale...@googlegroups.com
Dear Chris,

Thanks for your prompt replies and attention! 

The Functions._ import is useful.

i'm really looking for the text of the entire subtree at the location i indicated with my example. 

Your example 

val xpath = top(doc.rootElem).\*("sound"l) // we are already at 
myContainer, tempted to implicit top as well for 0.5 
println(text(xpath)) // works the same for any instance in TextImplicits

makes two presuppositions:
  • that myContainer is the top of the document
  • that <sound>...</sound> occurs inside <myContainer>...</myContainer>. 
My path expression

ScalesXPath("//myContainer/*[1]").withNameConversion(ScalesXPath.localOnly).evaluate(top(doc3))

makes neither of these. For example, if i load

<myRoot>
  <myContainer>
    <sound>
      <grenade>"BOOM!"</grenade>
    </sound>
    <fury>
      <nick>"S.H.I.E.L.D."</nick>
    </fury>
  </myContainer>
</myRoot>

into BaseX 7.X as a database and then type

//myContainer/*[1]

what i get back is

<sound>
  <grenade>"BOOM!"</grenade>
</sound>

i need to hand the text of the entire subtree (<sound>...</sound>) to a component that will process the rest.

Best wishes,

--greg

Chris Twiner

unread,
Jan 28, 2013, 5:08:52 PM1/28/13
to scale...@googlegroups.com
Hiya,

to be really sure what your asking about (element "text" has a fairly
typical meaning for xml):

1) you'd actually like the serialized xml, not its text value i.e.
asString(xpath.tree)
2) you don't want {}sound but whatever the first child element of all
myContainers is - so either //myContainer/*[1] or
asString(top(doc.rootElem).\\*("myContainer"l).\*(1).head.tree)

This would yield the serialised subtree xml with the default <?xml
version="1.0" encoding="UTF-8"?> serialization. Customising that
behaviour is via custom SerializerFactory instances.

re prompt reply - you are more than welcome :) I enjoyed your channel
9 videos, its nice to contribute something back.

Cheers,
Chris

On Mon, Jan 28, 2013 at 9:36 PM, Lucius Meredith
> --
> You received this message because you are subscribed to the Google Groups
> "scales-xml" group.
> To unsubscribe from this group, send email to
> scales-xml+...@googlegroups.com.
> For more options, visit https://groups.google.com/groups/opt_out.
>
>

Lucius Meredith

unread,
Jan 28, 2013, 5:33:52 PM1/28/13
to scale...@googlegroups.com
Dear Christian,

Brilliant! Many thanks for your patience and diligence.

Best wishes,

--greg
Reply all
Reply to author
Forward
0 new messages