firstchild has no properties - xml - firefox

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Noddy
    New Member
    • Jun 2007
    • 2

    firstchild has no properties - xml - firefox

    I am trying to parse some XML in Javascript on firefox. I get the error ‘firstchild has no properties’.

    The XML looks like this: -

    - <AddressSearc h>
    - <Summary>
    <Total>30</Total>
    <Pages>1</Pages>
    <Page>1</Page>
    <PageLength>200 </PageLength>
    - <Range>
    <Start>1</Start>
    <End>30</End>
    </Range>
    - <InputValues>
    <PostCode>sk1 4 6ld</PostCode>
    </InputValues>
    </Summary>
    - <Address>
    <PrimaryName />
    <PrimaryNumber> 12A</PrimaryNumber>
    <UPRN>100011577 044</UPRN>
    <DESCRIPTION>MO TTRAM MOOR</DESCRIPTION>
    <LOC />
    <TOWN>MOTTRAM </TOWN>
    <POSTTOWN>HYD E</POSTTOWN>
    <POSTCODE>SK1 4 6LD</POSTCODE>
    <X>399595</X>
    <Y>395775</Y>
    </Address>

    My code is :-

    AddressElements = rootNode.childN odes.item(1);

    if (browsername == "Firefox") {
    Northing = AddressElements .childNodes.ite m(en).firstChil d.nodeValue;
    } else {
    Northing = AddressElements .childNodes.ite m(en).text;
    }

    This works ok until the code hits the XML line <PrimaryName />

    If I check for the nodename equal to fields that are always present ( X for example) the code works ok.

    How can I check for the node having no properties?
  • acoder
    Recognized Expert MVP
    • Nov 2006
    • 16032

    #2
    Welcome to TSDN!

    Check for the firstChild property:
    [CODE=javascript]if (node.firstChil d) ...[/CODE]

    Comment

    • gregerly
      Recognized Expert New Member
      • Sep 2006
      • 192

      #3
      Firefox treats white space as children. Therefor, you can not use "firstChild " because in firefox, the first child may be white space. You would be better off doing something like this (using the summary tag as the main source of info)

      Code:
      <script type='text/javascript'>
      var summary = document.getElementsByTagName('Summary')[0];//grab the first summary tag
      var total = summary.getElementsByTagName('Total')[0];//grab the first Total tag within the summary tag
      var totalValue = total.firstChild.nodeValue;
      alert(totalValue);//should alert '30'
      </script>
      Using the getElementsByTa gName will always work, where as firstChild may or may not (depending on browser)

      Greg

      Comment

      • Noddy
        New Member
        • Jun 2007
        • 2

        #4
        thanks for the welcome and the help Greg, but it still fails for the empty node 'PrimaryName' - this field may or may not have data in it.

        Comment

        • gits
          Recognized Expert Moderator Expert
          • May 2007
          • 5390

          #5
          hi ...

          when testing for existence you may (should ;) ) always use:

          [CODE=javascript]
          if (typeof node_list[idx] != 'undefined') {
          // do something
          } else {
          // do something other
          }
          [/CODE]
          kind regards ...

          Comment

          Working...