SitePoint Sponsor

User Tag List

Results 1 to 3 of 3

Hybrid View

  1. #1
    SitePoint Evangelist
    Join Date
    Sep 2006
    Posts
    428
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Well... I thought I knew how to parse XML....

    Hi All! I've done a few projects lately in which I've been sucking in xml product data and parsing it via javascript.... BEST THING SINCE SLICED BREAD!!!

    Anyways I met an XML doc last night that I have no clue how to parse....? Any guidance or pointers would be greatly appreciated.

    Here's a snippet of the XML
    Code:
    <temperature type="maximum" units="Fahrenheit" time-layout="k-p24h-n7-1">
    <name>Daily Maximum Temperature</name>
    <value>84</value>
    <value>85</value>
    <value>86</value>
    <value>84</value>
    <value>86</value>
    <value>87</value>
    <value>87</value>
    </temperature>
    −
    <temperature type="minimum" units="Fahrenheit" time-layout="k-p24h-n6-2">
    <name>Daily Minimum Temperature</name>
    <value>64</value>
    <value>64</value>
    <value>64</value>
    <value>64</value>
    <value>66</value>
    <value>68</value>
    </temperature>
    This would be my 'normal' way of accessing a particular node on the tree. But how would I access the attributes in the <temperature> tag such as type units..etc?
    Code:
    var hiTemp = doc.getElementsByTagName('value')[0].firstChild.data;
    var hiTemp2 = doc.getElementsByTagName('value')[1].firstChild.data;
    Also, since there are several parents that use the value tag name, instead of doc.getElementsByTagName('value')[532].firstChild.data is there a way that I could traverse the tree by referencing the value node by the parent?

    for instance... doc.getElementsByTagName('temperature')[2].('value')[3].firstChild.data

    I know this is not right, but hopefully it will illustrate what I'm attempting to accomplish.

    Thanks in advance!

  2. #2
    SitePoint Wizard gRoberts's Avatar
    Join Date
    Oct 2004
    Location
    Birtley, UK
    Posts
    2,439
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Code javascript:
    var doc = null; // load your document
    var temperatures = doc.getElementsByTagName('temperature');
     
    /* gets the first temperature and gets the type attribute */
    var temp1type = temperatures[0].getAttribute('type');
    /* gets the first temperature and gets the units attribute */
    var temp1units = temperatures[0].getAttribute('units');
     
    /* gets the first temperature and gets the value of the name element within */
    var temp1name = temperatures[0].firstChild.nodeValue;
     
    /* gets the first temperature and gets all the value elements within */
    var temp1values = temperatures[0].getElementsByTagName('value');
     
    /* gets the value 84 from the first value element within the first temperature element */
    var temp1value1value = temp1values[0].nodeValue;

    I haven't tested it but thats roughly what i'd do. Your best off looking up in google for any references.


  3. #3
    SitePoint Evangelist
    Join Date
    Sep 2006
    Posts
    428
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Looks good to me! I'll give it a go tonight. Thanks!


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •