From Friday, April 19th (11:00 PM CDT) through Saturday, April 20th (2:00 PM CDT), 2024, ni.com will undergo system upgrades that may result in temporary service interruption.

We appreciate your patience as we improve our online experience.

LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Create Multiple XML Elements

Solved!
Go to solution

Hi Friends!

I have searched the forums high and low and cannot find something that guided me to a solution.  I am noob at XML (not at LabVIEW) so am trying to learn XML as I try to solve this problem which would seem to be a common one.  I have gone through multiple iterations of code and have not been able to solve it.  There is a solution in the forums that solves a similar problem for using multiple attributes.  Unfortunately my understanding of XML in LabVIEW is so weak that I was not able to extend that solution to resolve my problem.  Also, for reasons similar to those of DianeS, I cannot use the EasyXML, the LabXML VIs, or any other non-shipping library.

 

I am trying to create an XML file which has multiple elements each of which has elements within themselves.  As like this:

<?xml version="1.0" encoding="UTF-8" standalone="no" ?>

<board>

  <name>BOARD1</name>

  <Register>
    <Name>Register1</Name>
    <Offset>20</Offset>
    <Value>25</Value>
  </Register>
  <Register>
    <Name>Register2</Name>
    <Offset>20</Offset>
    <Value>25</Value>
  </Register>

</board>

 

I'm attaching the code which illustrates the problem.  If "NumberOfRegisters" is set to 1, then the code works, if it is set to higher, then an error 2602 results.  I know the code I'm attaching doesn't create the entire file as I've typed in.  I'm trying to get help on the core and figure that I'll be able to extend that solution to then be able to create the other parts that surround that.AttemptToCreateMultipleElements.png

The Helper VI is included in the examples but I thought I'd paste it here for convenience, I'm not sure how  a snippet's front panel works so I'm including the code as well.

 

thanks!

 C

0 Kudos
Message 1 of 7
(6,000 Views)

You get this error because an XML file can only have one root node. In your example register is the root node and all the other one should be child of this node.

 

What you can do is to add a "registers" root node that contains the register nodes. Like this:

 

Add multiple registers XML.png

 

Ben64

 

EDIT: It your case registers will be the BOARD node

 

 

Message 2 of 7
(5,965 Views)

Ben64,

Thanks very much for the great advice and information.  You definitely answered the question that I asked.  When I implemented the changes you indicated, there was a strange behavior in that the opening tags for each Register element had a trailing slash, and there were no closing tags for them.

<?xml version="1.0" encoding="UTF-8" standalone="no" ?>
<Registers>

  <Register/>

  <Name>Register0</Name>

  <Offset>0</Offset>

  <Value>0</Value>

  <Register/>

  <Name>Register1</Name>

  <Offset>0</Offset>

  <Value>0</Value>

</Registers>

A little bit of googling led me to this document, which indicates that a trailing slash on an opening tag indicates an empty element.  Thus the problem was one of ownership.  Further debugging and lavish use of the "Get XML (Pretty Print)" function led me to some final tweaks on the code.  I am attaching it here for the benefit of future users.  The final pretty printed xml document looks like this. Note that spacing is different too.

 

<?xml version="1.0" encoding="UTF-8" standalone="no" ?>
<Registers>

  <Register>
    <Name>RegName0</Name>
    <Offset>46</Offset>
    <Value>22</Value>
  </Register>

  <Register>
    <Name>RegName1</Name>
    <Offset>46</Offset>
    <Value>22</Value>
  </Register>

</Registers>

 

I've annotated the version I ended up with, pointing out the changes from Ben64's version.

Thanks again!

 C

CreateMultipleElementsFinal.png

0 Kudos
Message 3 of 7
(5,936 Views)
Solution
Accepted by topic author carlos_camargo

My error, here is the correct way:

 

add multiple nodes XML-2.png

 

Ben64

 

EDIT: OK, I just saw you identified the error source.

Message 4 of 7
(5,933 Views)

Thanks for following up, I accepted your updated answer as the correct one.  I noticed too from looking at your update that I had neglected to close one of my references.  Like I said, I'm noob at XML Smiley Happy.

 C

0 Kudos
Message 5 of 7
(5,927 Views)

Don't worry, it's still confusing when you're not new at it 🙂

0 Kudos
Message 6 of 7
(5,922 Views)

Hello all,

do you mind me asking at this point how loading the xml data would look like for this example?

Best

labviewette

0 Kudos
Message 7 of 7
(5,611 Views)