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:


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!


  • 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");
      String[] children = xml.listChildren();
      for (String childName : children) 
        if (!childName.startsWith("#"))
          XML child = xml.getChild(childName);
          float value = child.getFloatContent();
          println(childName + " " + value);

    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.