Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    New to the CF scene
    Join Date
    Apr 2013
    Posts
    3
    Thanks
    1
    Thanked 0 Times in 0 Posts

    how get attribute value from xml

    hi
    i have this xml code:

    assignment name="hw1">
    <course name="CSE 154"/>
    <due>
    <date>Wed Apr 8</date>
    <time>11:30pm</time>
    </due>

    <submissions>
    <submission studentid="91" >
    <date lateness="3">Sat Apr 11</date>
    <time>11:20pm</time>
    </submission>
    <submission studentid="26">
    <date lateness="0">Mon Apr 6</date>
    <time>9:44pm</time>
    </submission>
    <submission studentid="193">
    <date lateness="0">Sun Apr 5</date>
    <time>10:29pm</time>
    </submission>

    how can i get the value of lateness attribute??
    i try this but i can't get any results..

    <?php
    $doc = new DOMDocument();
    $doc->load( 'hw.xml' );

    $submissions = $doc->getElementsByTagName( "submission" );

    foreach( $submissions as $submission )
    {


    $lateness =$submission->$date.getAttribute('lateness');



    $times= $submission->getElementsByTagName( "time" );
    $time= $times->item(0)->nodeValue;


    echo "<b> $lateness - $time\n</b><br>";
    }
    ?>

  • #2
    Senior Coder
    Join Date
    Jan 2011
    Location
    Missouri
    Posts
    4,096
    Thanks
    23
    Thanked 594 Times in 593 Posts
    user99 your xml is not right. All tags must be closed
    assignment name="hw1">
    <course name="CSE 154"/>
    Are not and there is an extra / in the course tag. I re-wrote your xml and answered your question
    how get attribute value from xml
    and through in some others things for ya:
    PHP Code:
    <?php
    $xmlString 
    = <<<XML
    <submissions>
        <assignment name="hw1">
            <course name="CSE 154">
            <due>
                <date>Wed Apr 8</date>
                <time>11:30pm</time>
            </due>
            </course>
        </assignment>

        <submission studentid="91" >
            <date lateness="3">Sat Apr 11</date>
            <time>11:20pm</time>
        </submission>
        <submission studentid="26">
            <date lateness="0">Mon Apr 6</date>
            <time>9:44pm</time>
        </submission>
        <submission studentid="193">
            <date lateness="0">Sun Apr 5</date>
            <time>10:29pm</time>
        </submission>
    </submissions>
    XML;

    $xmldata simplexml_load_string($xmlString);
    echo 
    "Assignment " $xmldata->assignment['name'] . " for course " $xmldata->assignment->course['name'];
    echo 
    " is due " $xmldata->assignment->course->due->date " by " $xmldata->assignment->course->due->time ".";
    foreach(
    $xmldata->submission as $item)
    {
       echo 
    "<p>";
            echo 
    "Student " $item['studentid']."<br />";
            echo 
    "Turned in on " $item->date."<br />";
            echo 
    "Late by " $item->date['lateness']." days<br />";
            echo 
    "At " $item->time."<br />";
       echo 
    "</p>";
    }        
    ?>
    Evolution - The non-random survival of random variants.

    "If you leave hydrogen alone, for long enough, it begins to think about itself."

  • Users who have thanked sunfighter for this post:

    user99 (04-22-2013)

  • #3
    New to the CF scene
    Join Date
    Apr 2013
    Posts
    3
    Thanks
    1
    Thanked 0 Times in 0 Posts

    question for this reply

    Quote Originally Posted by sunfighter View Post
    user99 your xml is not right. All tags must be closed
    assignment name="hw1">
    <course name="CSE 154"/>
    Are not and there is an extra / in the course tag. I re-wrote your xml and answered your question and through in some others things for ya:
    PHP Code:
    <?php
    $xmlString 
    = <<<XML
    <submissions>
        <assignment name="hw1">
            <course name="CSE 154">
            <due>
                <date>Wed Apr 8</date>
                <time>11:30pm</time>
            </due>
            </course>
        </assignment>

        <submission studentid="91" >
            <date lateness="3">Sat Apr 11</date>
            <time>11:20pm</time>
        </submission>
        <submission studentid="26">
            <date lateness="0">Mon Apr 6</date>
            <time>9:44pm</time>
        </submission>
        <submission studentid="193">
            <date lateness="0">Sun Apr 5</date>
            <time>10:29pm</time>
        </submission>
    </submissions>
    XML;

    $xmldata simplexml_load_string($xmlString);
    echo 
    "Assignment " $xmldata->assignment['name'] . " for course " $xmldata->assignment->course['name'];
    echo 
    " is due " $xmldata->assignment->course->due->date " by " $xmldata->assignment->course->due->time ".";
    foreach(
    $xmldata->submission as $item)
    {
       echo 
    "<p>";
            echo 
    "Student " $item['studentid']."<br />";
            echo 
    "Turned in on " $item->date."<br />";
            echo 
    "Late by " $item->date['lateness']." days<br />";
            echo 
    "At " $item->time."<br />";
       echo 
    "</p>";
    }        
    ?>
    if we want to get more details about the students for different course how can i make the
    course to be parameterized?

  • #4
    Senior Coder
    Join Date
    Jan 2011
    Location
    Missouri
    Posts
    4,096
    Thanks
    23
    Thanked 594 Times in 593 Posts
    What information do you want about the students? How do you want that broken down between the courses?

    Maybe something like this?
    for example:
    Code:
    <student_info>
    	<student name="First Student" amount_courses="4">
    		<course_name>Math</course_name>
    		<course_grade>88</course_grade>
    
    		<course_name>Spanish</course_name>
    		<course_grade>84</course_grade>
    
    		<course_name>French</course_name>
    		<course_grade>92</course_grade>
    
    		<course_name>Biology</course_name>
    		<course_grade>76</course_grade>
    	</student>
    	
    	<student name="Second Student" amount_courses="2">
    		<course_name>Biology</course_name>
    		<course_grade>98</course_grade>
    
    		<course_name>Physics</course_name>
    		<course_grade>99</course_grade>
    	</student>
    </student_info>
    Evolution - The non-random survival of random variants.

    "If you leave hydrogen alone, for long enough, it begins to think about itself."

  • #5
    New Coder
    Join Date
    May 2013
    Location
    USA
    Posts
    40
    Thanks
    4
    Thanked 2 Times in 2 Posts
    XML DOM getAttribute() Method

    Definition and Usage of it
    The getAttribute() method gets an attribute value by name.

    Syntax
    elementNode.getAttribute(name)

    Parameter Name
    Description : Required. Specifies the attribute to get the attribute value from

    Example
    The following code fragment loads "books.xml" into xmlDoc using loadXMLDoc() and gets the value of the "category" attribute in all <book> elements:

    Example

    xmlDoc=loadXMLDoc("books.xml");

    x=xmlDoc.getElementsByTagName('book');

    for (i=0;i<x.length;i++)
    {
    document.write(x[i].getAttribute('category'));
    document.write("
    ");
    }
    Output:

    COOKING
    CHILDREN
    WEB
    WEB


  •  

    Posting Permissions

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