I have simple question using simpleXML and PHP

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • SM

    #1

    I have simple question using simpleXML and PHP

    Hello
    I have simple question using simpleXML and PHP.

    i have an xml file that looks like this:

    <?xml version="1.0" encoding="UTF-8"?>
    <discography version="0.01">
    <CD>
    <title>Moonligh t</title>
    <year>1978</year>
    <company>RCA</company>
    <item label="CD 1:">
    <title>Moonligh t</title>
    <title>Take It</title>
    <title>My Forever</title>
    <title>Anymor e</title>
    </item>
    <item label="Cd 2:">
    <title>All Right</title>
    <title>No More</title>
    <title>Betwee n</title>
    <title>Why</title>
    </item>
    </CD>
    </discography>
    Note that there could be more then one <itemtag. In this case, there
    is 2, but it could be 1 or 5...

    Using php, the output should look like this:

    Moonlight
    1978
    RCA

    CD 1:
    *Moonlight
    *Take It
    *My Forever
    *Anymore

    CD 2:
    *All Right
    *No More
    *Between
    *Why


    I'm almost there, but i don't know how to loop to get the <title>'s of
    al the <itemtags and the attribute of all the <item>'s tag.

    Here's my code:
    <?php
    if (file_exists('d b/discography/cd_1.xml')) {
    $xml = simplexml_load_ file('db/discography/cd_1.xml');
    }
    else {
    exit('Error opening the XML file.');
    }

    $title = $xml->CD->title;
    $year = $xml->CD->year;
    $company = $xml->CD->company;
    ?>

    <h1><?php echo $title; ?></h1>
    <h3><?php echo $year; ?></h3>
    <h3><?php echo $company; ?></h3>

    //get the <title>'s of all the <item>
    <ul>
    <?php
    foreach ($xml->CD->item as $item) {
    ?>
    <li><?php echo $item->title; ?></li>
    <?php
    }
    ?>
    </ul>

    My code will output:
    Moonlight
    1978
    RCA

    *Moonlight

    How do i loop through all the <titlein al lthe <item>'s that exist
    in the xml + how do i get the attribute of the <itemfile??

    Help!
    Thanks
    Marco
  • =?ISO-8859-1?Q?=22=C1lvaro_G=2E_Vicario=22?=

    #2
    Re: I have simple question using simpleXML and PHP

    SM escribió/wrote:
    [...]
    //get the <title>'s of all the <item>
    <ul>
    <?php
    foreach ($xml->CD->item as $item) {
    ?>
    <li><?php echo $item->title; ?></li>
    <?php
    }
    ?>
    </ul>
    [...]
    How do i loop through all the <titlein al lthe <item>'s that exist
    in the xml + how do i get the attribute of the <itemfile??
    It's very easy if you know the actual contents of the variables you
    handle. Add this line to the loop and you'll understand the issue:

    var_dump($item) ;



    --
    -- http://alvaro.es - Álvaro G. Vicario - Burgos, Spain
    -- Mi sitio sobre programación web: http://bits.demogracia.com
    -- Mi web de humor al baño María: http://www.demogracia.com
    --

    Comment

    • Janwillem Borleffs

      #3
      Re: I have simple question using simpleXML and PHP

      SM schreef:
      How do i loop through all the <titlein al lthe <item>'s that exist
      in the xml + how do i get the attribute of the <itemfile??
      >
      <?php
      foreach ($xml->CD->item as $item) {
      foreach ($item->title as $value) {
      ?>
      <li><?php echo $value; ?></li>
      <?php
      }
      }
      ?>

      JW

      Comment

      • SM

        #4
        Re: I have simple question using simpleXML and PHP

        On May 8, 5:20 am, Janwillem Borleffs <j...@jwscripts .comwrote:
        SM schreef:
        >
        How do i loop through all the <titlein al lthe <item>'s that exist
        in the xml + how do i get the attribute of the <itemfile??
        >
        <?php
        foreach ($xml->CD->item as $item) {
        foreach ($item->title as $value) {
        ?>
        <li><?php echo $value; ?></li>
        <?php
        }
        }
        ?>
        >
        JW
        Thanks JW & Alvaro:
        I've decided on JW solution. I couldn't understand the var_dump
        solution. I was also able to get the attributes.. Anyways, if it
        helps, here's the code:

        Thanks JW & Alvaro:
        I've decided on JW solution. I couldn't understand the var_dump
        solution. I was also able to get the attributes.. Anyways, if it
        helps, here's the final code:


        ....
        <h1>Tracks</h1>
        <?php
        foreach ($xml->CD->item as $item) {
        ?>
        <h3><?php echo $item['label']; ?></h3>
        <ul>
        <?php foreach ($item->title() as $track) {
        ?>
        <li><?php echo $track; ?></li>
        <?php
        }
        ?>
        </ul>
        <?php
        }
        ?>
        ....

        The output:

        Moonlight
        1978
        RCA

        CD 1:
        Moonlight
        Take It
        My Forever
        Anymore

        CD 2:
        All Right
        No More
        Between
        Why

        Comment

        • Jerry Stuckle

          #5
          Re: I have simple question using simpleXML and PHP

          SM wrote:
          On May 8, 5:20 am, Janwillem Borleffs <j...@jwscripts .comwrote:
          >SM schreef:
          >>
          >>How do i loop through all the <titlein al lthe <item>'s that exist
          >>in the xml + how do i get the attribute of the <itemfile??
          ><?php
          > foreach ($xml->CD->item as $item) {
          > foreach ($item->title as $value) {
          > ?>
          > <li><?php echo $value; ?></li>
          > <?php
          > }
          > }
          >?>
          >>
          >JW
          >
          Thanks JW & Alvaro:
          I've decided on JW solution. I couldn't understand the var_dump
          solution. I was also able to get the attributes.. Anyways, if it
          helps, here's the code:
          >
          <code snipped>

          Alvaro was trying to help you in a more general way. var_dump()
          displays the contents of the array, including the indexes and values.
          From there you have a lot better shot at figuring out how to build the
          code you need to use the array. Then the next time you run into a
          problem with arrays or other complex objects (whether simpleXML or
          something else), you can solve it more easily.


          --
          =============== ===
          Remove the "x" from my email address
          Jerry Stuckle
          JDS Computer Training Corp.
          jstucklex@attgl obal.net
          =============== ===

          Comment

          • SM

            #6
            Re: I have simple question using simpleXML and PHP

            On May 8, 11:01 am, Jerry Stuckle <jstuck...@attg lobal.netwrote:
            SM wrote:
            On May 8, 5:20 am, Janwillem Borleffs <j...@jwscripts .comwrote:
            SM schreef:
            >
            >How do i loop through all the <titlein al lthe <item>'s that exist
            >in the xml + how do i get the attribute of the <itemfile??
            <?php
            foreach ($xml->CD->item as $item) {
            foreach ($item->title as $value) {
            ?>
            <li><?php echo $value; ?></li>
            <?php
            }
            }
            ?>
            >
            JW
            >
            Thanks JW & Alvaro:
            I've decided on JW solution. I couldn't understand the var_dump
            solution. I was also able to get the attributes.. Anyways, if it
            helps, here's the code:
            >
            <code snipped>
            >
            Alvaro was trying to help you in a more general way. var_dump()
            displays the contents of the array, including the indexes and values.
            From there you have a lot better shot at figuring out how to build the
            code you need to use the array. Then the next time you run into a
            problem with arrays or other complex objects (whether simpleXML or
            something else), you can solve it more easily.
            >
            --
            =============== ===
            Remove the "x" from my email address
            Jerry Stuckle
            JDS Computer Training Corp.
            jstuck...@attgl obal.net
            =============== ===
            Thanks for the explanation. It's a good tip for debugging...
            Marco

            Comment

            Working...