top

XML parser’s white space processing

Published 2014-10-27 | (Compatible with SDK 3.5,4.5,5.0,5.1 and 2012,2013,2014 models)

Differences in text nodes processing on the 2012 platform.

Processing of white space (tab, newline, space) with respect to Node Recognition is different at the time of XML parsing.

As a solution, white space node processing function is added at the time of XML Data parsing. Browser wise processing method for White Space is different. In case of Webkit, it skips the empty fields.

The below is a Sample XML for Parsing.

<xml>
    <field>
        <id>Customer_ID</id>
        <value>ADAMS J</value>
    </field>
    <field>
        <id>First_Name</id>
        <value>John</value>
    </field>
    <field>
        <id>Last_Name</id>
        <value>  </value> <!--Two spaces-->
    </field>
    <field>
        <id>Business</id>
        <value>Programmers 'R' Us</value>
    </field>
    <field>
        <id>Phone_No</id>
        <value>     </value> <!--A Tab-->
    </field>
</xml>

Source Files

Note

The files needed for the sample application are here.

Bad example

var message = MessageBody[0].firstChild ? MessageBody[0].firstChild.nodeValue : 'default Value';

Good example

var tmpFirstChild = getFirstChild(MessageBody[0]) ;
var message = tmpFirstChild ? tmpFirstChild.nodeValue : 'default value';

function getFirstChild (tmpnode) {
    var tmpChild=tmpnode.firstChild;
        while (tmpChild) {
            if( (tmpChild.nodeType == 8) || ((tmpChild.nodeType == 3) && !(/[^\t\n\r\ ]/.test(tmpChild.data)))) {
                tmpChild = tmpChild.nextSibling;
            } else {
                return tmpChild;
            }
        }
    return null;
}