SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    SitePoint Evangelist
    Join Date
    Jul 2002
    Location
    Italy
    Posts
    425
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Reading XML file

    I'm trying to read an XML file and have got so far but need some advice.

    My XML file is
    Code:
    <?xml version="1.0"?><!DOCTYPE quiz [
        <!ELEMENT quiz (title, items)>
        <!ELEMENT title (#PCDATA)>
        <!ELEMENT items (item)+>    
        <!ELEMENT item (question, answer, answer+)>
        <!ELEMENT question (#PCDATA)>
        <!ELEMENT answer (#PCDATA)>
        <!ATTLIST answer correct (y) #IMPLIED>
    ]>
    <quiz>
        <title>Alberta Sparrow</title>
        <items>
            <item>
                <question>What type of bird is Alberta and her family?</question>
                <answer>a robin</answer>
                <answer>a duck</answer>
                <answer correct="y">a sparrow</answer>
                <answer>a blue jay</answer>
            </item>
          </items>
    </quiz>
    with several <item>s

    My PHP is
    PHP Code:
    $doc = new DOMDocument();
    $doc->load('quiz_f487t.xml');
    $items $doc->getElementsByTagName("item");
    foreach ( 
    $items as $item ) {
      
    $questions $item->getElementsByTagName("question");
      
    $question $questions->item(0)->nodeValue;
     
    $answers $item->getElementsByTagName("answer");
      
    $answer1 $answers->item(0)->nodeValue;
      
    $answer2 $answers->item(1)->nodeValue;
      
    $answer3 $answers->item(2)->nodeValue;
      
    $answer4 $answers->item(3)->nodeValue;
      echo 
    "<p>$question<br \> 1. $answer1<br \> 2. $answer2<br \> 3. $answer3<br \> 4. $answer4</p>\n";

    What I want is (a) to print the <title> and (b) to be able to tell which <answer> has the correct="y" attribute.

    Many thanks
    blessed are the cracked for they let in the light

  2. #2
    SitePoint Enthusiast
    Join Date
    May 2006
    Posts
    65
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    For the title after
    PHP Code:
    $items $doc->getElementsByTagName("item"); 
    add

    PHP Code:
    $title $doc->getElementsByTagName("title"); 

    Here is a great read for some more information. There are several parts and a prefence of mine is XML Reader

    http://www.ibm.com/developerworks/xm...hp1/index.html

  3. #3
    SitePoint Addict kduv's Avatar
    Join Date
    May 2012
    Location
    Maui, HI
    Posts
    211
    Mentioned
    5 Post(s)
    Tagged
    0 Thread(s)
    Also, if you'd rather work with the XML as an array, a very simple way to convert it is using the json_encode/decode functions.
    PHP Code:
    $array json_decode(json_encode(file_get_contents('filename.xml')), true); 

  4. #4
    SitePoint Evangelist
    Join Date
    Jul 2002
    Location
    Italy
    Posts
    425
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks Onle.

    Funnily enough it was an IBM article that got me as far as I got. I tried

    PHP Code:
    $title $doc->getElementsByTagName("title"); 
    but couldn't do anything with $title. Then I found I could print it if I used

    PHP Code:
    $title $doc->getElementsByTagName('title')->item(0)->nodeValue
    blessed are the cracked for they let in the light

  5. #5
    SitePoint Evangelist
    Join Date
    Jul 2002
    Location
    Italy
    Posts
    425
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks, kduv. I'll have a play with that too.
    blessed are the cracked for they let in the light

  6. #6
    SitePoint Enthusiast
    Join Date
    May 2006
    Posts
    65
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Good catch gandalf, glad you got it working.

  7. #7
    SitePoint Evangelist
    Join Date
    Jul 2002
    Location
    Italy
    Posts
    425
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I fathomed out the 2nd part of my query too. To get the 'correct' attribute:
    PHP Code:
    $correct1 $answers->item(0)->getAttribute('correct'); 
    G
    blessed are the cracked for they let in the light


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
  •