Contents from xml tag

Got a LiveCode personal license? Are you a beginner, hobbyist or educator that's new to LiveCode? This forum is the place to go for help getting started. Welcome!

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller

Post Reply
samjith
Posts: 85
Joined: Tue Mar 31, 2015 10:09 am

Contents from xml tag

Post by samjith » Thu Oct 27, 2016 11:37 am

Is it possible to get the node contents from this sample XML?

i/p
******

Code: Select all

<root>
<Tag1>just a tag</Tag1>
<Tag1>just a tag</Tag1>
<A att="10">
<text x="5"> some text 1</text>
<text x= "10"> some text 2</text>
</A>
<Tag1>just a tag</Tag1>
<A x="1"> some text 3</A>
<A x="2"> some text 4</A>
<Tag1>just a tag</Tag1>
<A att="0">
<text x = "15"> some text 5</text>
<text x = "10"> some text 6</text>
</A>
</root>
expected o/p
*******************
15: some text 1
20: some text 2
1: some text 3
2: some text 4
15: some text 5
20: some text 6
*******************
(this may confuse: if "A"contains "text" then, add the values eg: 10+5, and the result of "text")

MaxV
Posts: 1580
Joined: Tue May 28, 2013 2:20 pm
Contact:

Re: Contents from xml tag

Post by MaxV » Thu Oct 27, 2016 4:12 pm

Look this example:
this XML:

Code: Select all

<root>
<Tag1>just a tag</Tag1>
<Tag1>just a tag</Tag1>
<A att="10">
<text x="5"> some text 1</text>
<text x= "10"> some text 2</text>
</A>
<Tag1>just a tag</Tag1>
<A x="1"> some text 3</A>
<A x="2"> some text 4</A>
<Tag1>just a tag</Tag1>
<A att="0">
<text x = "15"> some text 5</text></A></root>
put this XML in a field and then in a button this code:


########CODE#######
on mouseUp
revXMLDeleteAllTrees
put field 1 into tXMLData
put revXMLCreateTree(tXMLData, true, true, true) into xmlID
put revXMLChildNames(XMLID,"/",comma,,true) # returns Tag1[1],Tag1[2],A[1],Tag1[3],A[2],A[3],Tag1[4],A[4]
put revXMLNodeContents(xmlID, "root/Tag1[1]") #returns "just a tag"
end mouseUp
#####END OF CODE#####

Now you can deal with your XML. :D
Livecode Wiki: http://livecode.wikia.com
My blog: https://livecode-blogger.blogspot.com
To post code use this: http://tinyurl.com/ogp6d5w

samjith
Posts: 85
Joined: Tue Mar 31, 2015 10:09 am

Re: Contents from xml tag

Post by samjith » Fri Oct 28, 2016 5:25 am

You deal me, in a nice way :D

I understood something. But i don't know how to treat with the attributes.
put revXMLChildNames(XMLID,"/",comma,,true) # returns Tag1[1],Tag1[2],A[1],Tag1[3],A[2],A[3],Tag1[4],A[4]
and why the "test" tag not return here

How you know these things?
Can you suggest some tutorials/examples/links, to extract data’s from the XML, for my requirements,
apart from http://lessons.livecode.com/m/4071/l/70 ... n-xml-file
because i am very new in revXML.... tags

grazie molto

[-hh]
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 2262
Joined: Thu Feb 28, 2013 11:52 pm

Re: Contents from xml tag

Post by [-hh] » Fri Oct 28, 2016 12:54 pm

You could try the following. You don't have to know anything of XML for that.
It's just an exercise with linedelimiter and itemdelimiter.
Not this elegant, written right out of my brain. But it works here.
[For your test input the output shows that your last example line is wrong: Should read "10: some text 6".]

Code: Select all

on mouseUp
  put fld 1 into t -- your input text
  set linedel to "<A "; set itemdel to "</A>"
  delete line 1 of t
  repeat for each line L in t
    put item 1 of L into s
    set itemdel to ">"; put item 1 of s into s1
    if "att" is not in s1 then
      put cr& "att="& quote&"0"&quote& "> " & s after s0
    else
      replace "=" with space in s1
      put "att="&(word -1 of s1)& "> " into z
      set linedel to "<text "; set itemdel to "</text>"
      delete line 1 of s
      repeat for each line l1 in s
        put cr & z & item 1 of l1 after s0
      end repeat
    end if
    set linedel to "<A "; set itemdel to "</A>"
  end repeat
  set linedel to cr; set itemdel to ">"
  repeat for each line L in s0
    if L is empty then next repeat
    put item 1 of L into i0; replace "=" with space in i0
    put value(word -1 of i0) into y
    put item 2 of L into i0; replace "=" with space in i0
    put value(word -1 of i0) into x
    put cr & (y+x) & colon & item 3 of L after t0
  end repeat
  put char 2 to -1 of t0 into fld 2 -- the output field
end mouseUp
For multichar delimiters as above you need to use LC 7 and above.
For LC 6 you can adjust the code by using an unusual char as temporary single-char delimiter, for example

Code: Select all

-- instead of
set linedel to "<A "; set itemdel to "</A>"
-- you do in LC 6 in order to have a single char delimiter
replace "<A " with numToChar(1) in t; set linedel to numToChar(1)
replace "</A> " with numToChar(2) in t; set itemdel to numToChar(2)
[Edit. Removed some typos.]
Last edited by [-hh] on Fri Oct 28, 2016 1:30 pm, edited 2 times in total.
shiftLock happens

MaxV
Posts: 1580
Joined: Tue May 28, 2013 2:20 pm
Contact:

Re: Contents from xml tag

Post by MaxV » Fri Oct 28, 2016 12:56 pm

This is my blog: http://livecodeitalia.blogspot.it/
and this is the result about XML: http://livecodeitalia.blogspot.it/search?q=xml
It's translated in your language by the google translator (top right button)
Livecode Wiki: http://livecode.wikia.com
My blog: https://livecode-blogger.blogspot.com
To post code use this: http://tinyurl.com/ogp6d5w

samjith
Posts: 85
Joined: Tue Mar 31, 2015 10:09 am

Re: Contents from xml tag

Post by samjith » Fri Oct 28, 2016 1:27 pm

@hh, Great work, but it may difficult to debug and maintain in future.

@MaxV thanks for your links.

Post Reply