Dan Davies
Dan Davies

Reputation: 73

Using php code to output the itemid in XML

I am trying to get my code to output the code in XML, however I'm unsure about how to get it to work properly. So far it outputs the code in php/html format.

This is my XML:

 <bookcollection>
 <items>
 <item id="483">
 <title>Database systems management and design/</title>
 <isbn>0877091153</isbn>
 <url>http://library.hud.ac.uk/catlink/bib/483</url>
 <borrowedcount>28</borrowedcount>
 <courses>
 <course>CC140</course>
 </courses>
 </item>
 </items>
 </bookcollection>

And the PHP:

<?php

$xmlassignDoc = new DOMDocument();
$xmlassignDoc->load("books.xml");

 $books = $xmlassignDoc->getElementsByTagName("item");

foreach($books as $list)
{
    $bookID = $list->getAttribute("id");


    //HERE is where the GET function will be
    if ($bookID == '483')
    {
        $id = $list->getAttribute("id");
        echo "<b>Book ID: </b> $id <br>";

         $title = $list->getElementsByTagName("title");
         $title = $title->item(0)->nodeValue;
         echo "<b>Title: </b> $title <br>";

         $isbn = $list->getElementsByTagName("isbn");
         $isbn = $isbn->item(0)->nodeValue;
         echo "<b>ISBN: </b> $isbn <br>";

         $borrowed = $list->getElementsByTagName("borrowedcount");
         $borrowed = $borrowed->item(0)->nodeValue;
         echo "<b>Borrowed Count: </b> $borrowed <br>";
         echo "<br>";


    } 
}

?>

Any ideas would help me a lot.

Upvotes: 0

Views: 175

Answers (2)

Ponsjuh
Ponsjuh

Reputation: 468

From what i understand you want to pull out a xml element and save it as another xml file

if so use this

$xmlassignDoc = new DOMDocument();
$xmlassignDoc->load("book.xml");

$books = $xmlassignDoc->getElementsByTagName("item");

$singleBook = new DOMDocument('1.0', 'utf-8');
$singleBook->formatOutput = true;

foreach($books as $book) {
    $bookID = $book->getAttribute("id");

    //HERE is where the GET function will be
    if ($bookID == '483') {
        $singleBook->loadXML("<book></book>");
        $node = $singleBook->importNode($book, true);
        $singleBook->documentElement->appendChild($node);
        echo $singleBook->saveXML(); //do we need to save?!?!
    } 
}

result:

<?xml version="1.0"?>
<book>
    <item id="483">
        <title>Database systems management and design 1</title>
        <isbn>0877091153</isbn>
        <url>http://library.hud.ac.uk/catlink/bib/483</url>
        <borrowedcount>28</borrowedcount>
        <courses>
            <course>CC140</course>
        </courses>
    </item>
</book>

or if you only want the id as a result

if ($bookID == '483') {
    $singleBook->loadXML("<book></book>");
    $node = $singleBook->createElement("id", $bookID); //<--- ID only
    $singleBook->documentElement->appendChild($node);
    echo $singleBook->saveXML();
}

result

<?xml version="1.0"?>
<book>
    <id>483</id>
</book>

In your case you want the output to be

<results>
    <book id="1234" title="Interaction Design" isbn="968347337" borrowedcount="15"/>
</results>

use this

$xmlassignDoc = new DOMDocument();
$xmlassignDoc->load("books.xml");

$books = $xmlassignDoc->getElementsByTagName("item");

$singleBook = new DOMDocument('1.0', 'utf-8');
$singleBook->formatOutput = true;

foreach($books as $book)
{
    $bookID = $book->getAttribute("id");

    //HERE is where the GET function will be
    if ($bookID == '483') {
        $singleBook->loadXML("<results></results>");
        $element = $singleBook->createElement("book");

        //id attribute
        $attrId = $singleBook->createAttribute('id');
        $attrId->value = $bookID;

        //title attribute
        $title = $book->getElementsByTagName("title");
        $title = $title->item(0)->nodeValue;
        $attrTitle = $singleBook->createAttribute('title');
        $attrTitle->value = $title;

        //the rest of the attributes

        //add the attributes to the element
        $element->appendChild($attrId);
        $element->appendChild($attrTitle);

        //add the element to the dox
        $singleBook->documentElement->appendChild($element);

        //output the result (don't forget to save if needed :) )
        echo $singleBook->saveXML();
    } 
}

Upvotes: 2

Prasanth Bendra
Prasanth Bendra

Reputation: 32740

Try this :

$xml = '<?xml version="1.0" encoding="UTF-8" standalone="no" ?>
 <bookcollection>
 <items>
 <item id="483">
 <title>Database systems management and design/</title>
 <isbn>0877091153</isbn>
 <url>http://library.hud.ac.uk/catlink/bib/483</url>
 <borrowedcount>28</borrowedcount>
 <courses>
 <course>CC140</course>
 </courses>
 </item>
 </items>
 </bookcollection>';

$array = json_decode(json_encode((array)simplexml_load_string($xml)),1);

echo "<pre>";
print_r($array);

To get id :

echo $id = $array['items']['item']['@attributes']['id'];

You will get the output as array, fetch the values from this array :

Array
(
    [items] => Array
        (
            [item] => Array
                (
                    [@attributes] => Array
                        (
                            [id] => 483
                        )

                    [title] => Database systems management and design/
                    [isbn] => 0877091153
                    [url] => http://library.hud.ac.uk/catlink/bib/483
                    [borrowedcount] => 28
                    [courses] => Array
                        (
                            [course] => CC140
                        )

                )

        )

)

Upvotes: 1

Related Questions