我试图让我的php代码以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>";
}
}
?>
任何想法都会对我有所帮助。
答案 0 :(得分: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();
}
}
答案 1 :(得分: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
)
)
)
)