Android Question XML file --> xml.parser -->changes-->xmlbuilder-->new.xml

emockler

Member
Licensed User
Longtime User
Hi,
I am trying to parse an existing XML, present certain fields for editing and write out a replacement.
I have the parsing working, started on "presenting" the fields, and thought I'd better check to see if I can duplicate what I have with the xmlbuilder.

I tend to copy pieces of code from questions regarding whatever I'm trying to do but the xmlbuilder content is kind of sparse. I looked at the library page, it has more options than were ever mentioned in any question.
I am able to produce lines like this:

<name>Win10</name>

But I am having trouble with the attributes like this:

<topology sockets='1' cores='2' threads='1'/>

Naturally there is a mixture of both in the XML (attached)
As you can see it's a VM definition file, which needs to be XML.

I'm still just testing, so for the xmlbuilder I have this copied from an old question. I didn't want to necro-post, so I started new. It's not personalized for me yet (seemed pretty generic tho):

B4X:
sub xml

Dim x As XMLBuilder
x = x.create("Data")
x = x.element("Field") _
.element("id").text("01") _
.up() _
.element("job_no").text("A001")

Dim props As Map
props.Initialize
props.Put("indent", "yes")
File.WriteString(File.DirRootExternal, "create.xml", x.asString2(props))

End Sub

Thanks!

Eric
 

Attachments

  • win10.xml
    4.2 KB · Views: 245

emockler

Member
Licensed User
Longtime User
Hi Erel, thanks for the example.
Seems to work. I made it generate this:

<domain xmlns:qemu="http://libvirt.org/schemas/domain/qemu/1.0" type="kvm" id="3">
<topology sockets="1" cores="2" threads="1"/>


when the original looks like this:
<domain type='kvm' id='3' xmlns:qemu='http://libvirt.org/schemas/domain/qemu/1.0'>
<topology sockets='1' cores='2' threads='1'/>

You can see my generated xml has these " where the original has these ', also it's out of order.
However, they can be a problem since they are for comments in b4a and my code was turning green, so I did some testing to see if it will accept the xml like this.

I replaced those 2 lines in an existing xml and did "virsh define test.xml" and the VM defined successfully and I could edit the xml via "virsh edit xml-test". And these " were changed to ' as well as the order fixed on the domain line. So the virsh define command must be more forgiving than I expected. So it does what I need, but I'm still curious as to why the order is off and how to do single quotes. Not desperately though..

Thanks!
Eric
 
Upvote 0
Top