#1
  1. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2001
    Location
    Madrid, Spain
    Posts
    4
    Rep Power
    0

    Javascript and XML attributes


    Hello,

    I am trying to do something with JavaScript that would be quite
    easy to do with XSL, but for reasons that would take to long to
    explain, I need to do it in JavaScript.

    Basically, If I have the following simple XML:

    <Questionaire>
    <Section title="section1">
    <Question>
    .
    .
    .
    </Question>
    </Section>
    <Section title="section2">
    <Question>
    </Question>
    </Section>
    </Questionaire>

    How do I extract/get the section attributes?

    I have the following Javascript to process the file:

    <HTML>
    <head><title>XML File output</title></head>
    <XML ID="xml_file" SRC="questionaire.xml"></XML>

    <BODY>
    <SCRIPT LANGUAGE="JavaScript1.1">

    function parse_xml( node )
    {
    section_list = xml_file.XMLDocument.selectNodes( "Questionaire/Section" );

    //Loop through the sections
    for( i = 0; i < section_list.length; i++ )
    {
    // I want to get the attribute here
    section_array = new section( section_list[i] );

    question_list = section_list[i].selectNodes( "Question" );

    // Now loop through each question in the section
    for( j = 0; j < question_list.length; j++ )
    {
    question_array= new question( question_list[j] );

    }
    }
    }
    </SCRIPT>
    </BODY>
    </HTML>

    I know that the logical solution would be to change the XML section
    definition to the following:
    <Section>
    <SectionTitle>section1</SectionTitle>
    <Question>
    </Question>
    </Section>

    But the other way is still standard XML and you should be able
    to get to the attribute with JavaScript.

    I tried different combinations of the following with no success
    selectSingleNode( Section[@title])

    Any Ideas???

    Brady
    Last edited by BJohnson; September 27th, 2001 at 05:53 AM.
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2001
    Location
    Belarus
    Posts
    30
    Rep Power
    14
    hi BJohnson

    how about this line

    var title = section_list[i].getAttribute("title")
  4. #3
  5. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2001
    Location
    Madrid, Spain
    Posts
    4
    Rep Power
    0

    Smile It works!!


    Alexandr,

    That's great, its just that simple!

    I have a book that explains XML and has some
    examples on how to use JavaScript with XML,
    but it doesnt include that method.

    I was also searching all over the internet
    looking for a Complete list of JavaScript
    XML functions. Do you know of anything.

    Thanks alot for your help.

    Brady

IMN logo majestic logo threadwatch logo seochat tools logo