B4J Question Looping through data, to populate an XMLBuilder object

B4JExplorer

Active Member
Licensed User
Longtime User
Hi,

I can build single or multiple nodes into an XMLBuilder object, but do not know how to successively add nodes and attributes during a loop.

So if we do




For n_NthID = 0 To n_NumberOfIDs

s_id = GetNthId( n_NthID )
xb_XMLBuilder = xb_XMLBuilder.element( "Index" ) _
.attribute( "Index_ID", s_id ) _
.up()

Next '// n_NthID = 0 To n_NumberOfIDs


, how do we keep adding to xb_XMLBuilder, during each loop?


(Sorry, I still cannot insert formatted code, into a forum message. The toolbar with fonts & formatting & such, are not visible in my message input box.)
 

rwblinn

Well-Known Member
Licensed User
Longtime User
Hi,

this example
B4X:
Sub XMLCreateNames
  Dim x As XMLBuilder
  x = x.create("Names").attribute("xmlns", "List of Names")
  x = x.element("Name1").element("ID").text("1").up().element("Name").text("Name 1").up().up()
  x = x.element("Name2").element("ID").text("2").up().element("Name").text("Name 2").up().up()
  Dim props As Map
  props.Initialize
  props.Put("{http://xml.apache.org/xslt}indent-amount", "2")
  props.Put("indent", "yes")
  Log(x.asString2(props))
End Sub
will create output
B4X:
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<Names xmlns="List of Names">
  <Name1>
    <ID>1</ID>
    <Name>Name 1</Name>
  </Name1>
  <Name2>
    <ID>2</ID>
    <Name>Name 2</Name>
  </Name2>
</Names>

The trick is using up().up().

Hint: if the formatting toolbar is not visible then insert manually the BB tags. For code these are CODE and /CODE in brackets []
 
Upvote 0

B4JExplorer

Active Member
Licensed User
Longtime User
Hi,

this example
B4X:
Sub XMLCreateNames
  Dim x As XMLBuilder
  x = x.create("Names").attribute("xmlns", "List of Names")
  x = x.element("Name1").element("ID").text("1").up().element("Name").text("Name 1").up().up()
  x = x.element("Name2").element("ID").text("2").up().element("Name").text("Name 2").up().up()
  Dim props As Map
  props.Initialize
  props.Put("{http://xml.apache.org/xslt}indent-amount", "2")
  props.Put("indent", "yes")
  Log(x.asString2(props))
End Sub
will create output
B4X:
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<Names xmlns="List of Names">
  <Name1>
    <ID>1</ID>
    <Name>Name 1</Name>
  </Name1>
  <Name2>
    <ID>2</ID>
    <Name>Name 2</Name>
  </Name2>
</Names>

The trick is using up().up().

Hint: if the formatting toolbar is not visible then insert manually the BB tags. For code these are CODE and /CODE in brackets []

rwb, I'm already able to do this. My problem is doing it incrementally, in a loop.

For example, if a string were being populated from a list, then you might concatenate each iteration of the string in the loop, like

B4X:
    s_String = s_String & " " & s_NthWord

So, my question is, how do you sort of 'concatenate' each section of the xml file, to the xmlbuilder object?


(Yep, that was it, thanks. The CODE in brackets. I couldn't remember it, and there doesn't seem to be any quick help, on the message page.)
 
Upvote 0

rwblinn

Well-Known Member
Licensed User
Longtime User
I am not sure I understand your question.
Instead of adding like I shared previous, doing this in a loop would be like below. Within the loop you can do anything prior adding, like concatenate strings.
B4X:
Dim x As XMLBuilder
x = x.create("Names").attribute("xmlns", "List of Names")
Dim s As String
For i = 1 To 2
  s = s & "Name " & i 
  x = x.element("Name"&i).element("Name").text(s).up().up()
Next
Dim props As Map
props.Initialize
props.Put("{http://xml.apache.org/xslt}indent-amount", "2")
props.Put("indent", "yes")
Log(x.asString2(props))
Output
B4X:
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<Names xmlns="List of Names">
  <Name1>
  <Name>Name 1</Name>
  </Name1>
  <Name2>
  <Name>Name 1Name 2</Name>
  </Name2>
</Names>
 
Upvote 0

B4JExplorer

Active Member
Licensed User
Longtime User
I am not sure I understand your question.
Instead of adding like I shared previous, doing this in a loop would be like below. Within the loop you can do anything prior adding, like concatenate strings.
B4X:
Dim x As XMLBuilder
x = x.create("Names").attribute("xmlns", "List of Names")
Dim s As String
For i = 1 To 2
  s = s & "Name " & i 
  x = x.element("Name"&i).element("Name").text(s).up().up()
Next
Dim props As Map
props.Initialize
props.Put("{http://xml.apache.org/xslt}indent-amount", "2")
props.Put("indent", "yes")
Log(x.asString2(props))
Output
B4X:
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<Names xmlns="List of Names">
  <Name1>
  <Name>Name 1</Name>
  </Name1>
  <Name2>
  <Name>Name 1Name 2</Name>
  </Name2>
</Names>
The results of that, are what I'm looking for, although I don't understand how the xmlbuilder object gets appended to. It looks like it keeps getting replaced.

Anyway, good enough, I can play with that, thanks rwblinn.
 
Upvote 0
Top