Determining the child count of a XML element where the children have unique names

edited May 2017 in How To...

I'm having problems with determining the number of children of an XML element, where the child elements don't have the same name / unique names.

This is part of the XML file:

<process>
    <f0>255.0000</f0>
    <f1>238.2833</f1>
    <f2>222.1333</f2>
    <f3>206.5500</f3>
    <f4>191.5333</f4>
    <f5>177.0833</f5>
    <f6>163.2000</f6>
    <f7>149.8833</f7>
    <f8>137.1333</f8>
    <f9>124.9500</f9>
    <f10>113.3333</f10>
    <f11>102.2833</f11>
    <f12>91.8000</f12>
    <f13>81.8833</f13>
    <f14>72.5333</f14>
    <f15>63.7500</f15>
    <f16>55.5333</f16>
    <f17>47.8833</f17>
    <f18>40.8000</f18>
    <f19>34.2833</f19>
    <f20>28.3333</f20>
    <f21>22.9500</f21>
    <f22>18.1333</f22>
    <f23>13.8833</f23>
    <f24>10.2000</f24>
    <f25>7.0833</f25>
    <f26>4.5333</f26>
    <f27>2.5500</f27>
    <f28>1.1333</f28>
    <f29>0.2833</f29>
    <f30>0.0000</f30>
</process>

Using process.getChildCount() I would expect it to return 31. But it does return 63. So does process.getChildren().length.
I found this thread about calling getChildren() without parameter which doesn't ignore whitespace.
Good to know, but why is not ignoring whitespace useful and wouldn't a parameter to turn on/off ignoring of whitespace be good here?

In most other threads I found it was simply suggested to call getChildren() with parameter to match exactly the child elements you need. But in my case this is not an option since all elements have different names.
So I could now loop through the array returned by process.getChildren(), while building a new array and use child.getName() to exclude elements that are empty or return #text from being added to the new array. But this is a lot of work I think and not the behavior I would expect.
Are there better solutions for this?

Finally I wonder why some elements using child.getName() return #text and some are empty.

Thanks for any comments on this!

Answers

  • Whitespace is part of XML so ignoring it was a mistake / an oversimplification of the former XML parser. Now Processing uses the standard XML parser which is standards compliant. You just have to learn how to distinguish these textual nodes from the others.

    For example, to handle your file, you can do:

    void setup() 
    {
      XML xml = loadXML("H:/Temp/Test.xml");
      println(xml);
      String[] children = xml.listChildren();
      println(children.length);
    
      for (String childName : children) 
      {
        if (!childName.startsWith("#"))
        {
          XML child = xml.getChild(childName);
          float value = child.getFloatContent();
          println(childName + " " + value);
        }
      }
      println("Done");
      exit();
    }
    

    Not sure if the # hack is the best way to handle this, though. A quick glance at the API doesn't show a simple way to get the type of an XML element.

  • @jvolker did you find any reasonable solution to this?

Sign In or Register to comment.