0

我试图让我的代码以 XML 格式输出代码,但是我不确定如何让它正常工作。到目前为止,它以 php/html 格式输出代码。

这是我的 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>

和 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>";


    } 
}

?>

任何想法都会对我有很大帮助。

4

2 回答 2

2

据我了解,您想提取一个 xml 元素并将其另存为另一个 xml 文件

如果是这样使用这个

$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?!?!
    } 
}

结果:

<?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>

或者如果你只想要 id 作为结果

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

结果

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

在您的情况下,您希望输出为

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

用这个

$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();
    } 
}
于 2013-02-27T12:52:22.657 回答
1

尝试这个 :

$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);

获取 id :

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

您将获得作为数组的输出,从该数组中获取值:

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
                        )

                )

        )

)
于 2013-02-27T12:41:29.817 回答