Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8239079
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T20:01:23+00:00 2026-06-07T20:01:23+00:00

I’m reading a simple xml file using matlab’s xmlread internal function. <root> <ref> <requestor>John

  • 0

I’m reading a simple xml file using matlab’s xmlread internal function.

<root>
    <ref>
        <requestor>John Doe</requestor>
        <project>X</project>
    </ref>
</root>

But when I call getChildren() of the ref element, it’s telling me that it has 5 children.

It works fine IF I put all the XML in ONE line. Matlab tells me that ref element has 2 children.

It doesn’t seem to like the spaces between elements.

Even if I run Canonicalize in oXygen XML editor, I still get the same results. Because Canonicalize still leaves spaces.

Matlab uses java and xerces for xml stuff.

Question:

What can I do so that I can keep my xml file in human readable format (not all in one line) but still have matlab correctly parse it?

Code Update:

filename='example01.xml';
docNode = xmlread(filename);
rootNode = docNode.getDocumentElement;
entries = rootNode.getChildNodes;
nEnt = entries.getLength
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T20:01:25+00:00Added an answer on June 7, 2026 at 8:01 pm

    The XML parser behind the scenes is creating #text nodes for all whitespace between the node elements. Whereever there is a newline or indentation it will create a #text node with the newline and following indentation spaces in the data portion of the node. So in the xml example you provided when it is parsing the child nodes of the “ref” element it returns 5 nodes

    1. Node 1: #text with newline and indentation spaces
    2. Node 2: “requestor” node which in turn has a #text child with “John Doe” in the data portion
    3. Node 3: #text with newline and indentation spaces
    4. Node 4: “project” node which in turn has a #text child with “X” in the data portion
    5. Node 5: #text with newline and indentation spaces

    This function removes all of these useless #text nodes for you. Note that if you intentionally have an xml element composed of nothing but whitespace then this function will remove it but for the 99.99% of xml cases this should work just fine.

    function removeIndentNodes( childNodes )
    
    numNodes = childNodes.getLength;
    remList = [];
    for i = numNodes:-1:1
       theChild = childNodes.item(i-1);
       if (theChild.hasChildNodes)
          removeIndentNodes(theChild.getChildNodes);
       else
          if ( theChild.getNodeType == theChild.TEXT_NODE && ...
               ~isempty(char(theChild.getData()))         && ...
               all(isspace(char(theChild.getData()))))
             remList(end+1) = i-1; % java indexing
          end
       end
    end
    for i = 1:length(remList)
       childNodes.removeChild(childNodes.item(remList(i)));
    end
    
    end
    

    Call it like this

    tree = xmlread( xmlfile );
    removeIndentNodes( tree.getChildNodes );
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

We are using XSLT to translate a RIXML file to XML. Our RIXML contains
I have just tried to save a simple *.rtf file with some websites and
I am reading a book about Javascript and jQuery and using one of the
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
In my XML file chapters tag has more chapter tag.i need to display chapters
I'm parsing an XML file, the creators of it stuck in a bunch social
I'm making a simple page using Google Maps API 3. My first. One marker
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.