使用php代码以XML格式输出itemid
我试图让我的代码以XML格式输出代码,但是我不确定如何让它正常工作。到目前为止,它以php/html格式输出代码 这是我的XML:使用php代码以XML格式输出itemid,php,xml,Php,Xml,我试图让我的代码以XML格式输出代码,但是我不确定如何让它正常工作。到目前为止,它以php/html格式输出代码 这是我的XML: <bookcollection> <items> <item id="483"> <title>Database systems management and design/</title> <isbn>0877091153</isbn> <url>http:
<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>
数据库系统管理与设计/
0877091153
http://library.hud.ac.uk/catlink/bib/483
28
CC140
而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>";
}
}
?>
任何想法都会对我有很大帮助。试试这个:
$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);
试试这个:
$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);
据我所知,您希望提取一个xml元素并将其保存为另一个xml文件 如果是这样的话,就用这个
$xmlsassigndoc=newDOMDocument();
$xmlsassigndoc->load(“book.xml”);
$books=$xmlsassigndoc->getElementsByTagName(“项目”);
$singleBook=新的DOMDocument('1.0','utf-8');
$singleBook->formatOutput=true;
foreach($books作为$book){
$bookID=$book->getAttribute(“id”);
//下面是GET函数的位置
如果($bookID='483'){
$singleBook->loadXML(“”);
$node=$singleBook->importNode($book,true);
$singleBook->documentElement->appendChild($node);
echo$singleBook->saveXML();//我们需要保存吗?!?!
}
}
结果:
数据库系统管理和设计1
0877091153
http://library.hud.ac.uk/catlink/bib/483
28
CC140
或者,如果您只需要id作为结果
if($bookID==“483”){
$singleBook->loadXML(“”);
$node=$singleBook->createElement(“id”,$bookID);//documentElement->appendChild($node);
echo$singleBook->saveXML();
}
结果
483
在您的情况下,您希望输出是
用这个
$xmlsassigndoc=newDOMDocument();
$xmlsassigndoc->load(“books.xml”);
$books=$xmlsassigndoc->getElementsByTagName(“项目”);
$singleBook=新的DOMDocument('1.0','utf-8');
$singleBook->formatOutput=true;
foreach($books作为$book)
{
$bookID=$book->getAttribute(“id”);
//下面是GET函数的位置
如果($bookID='483'){
$singleBook->loadXML(“”);
$element=$singleBook->createElement(“book”);
//id属性
$attrId=$singleBook->createAttribute('id');
$attrId->value=$bookID;
//标题属性
$title=$book->getElementsByTagName(“title”);
$title=$title->item(0)->nodeValue;
$attrTitle=$singleBook->createAttribute('title');
$attrtTitle->value=$title;
//其余的属性
//将属性添加到元素中
$element->appendChild($attrId);
$element->appendChild($attrTitle);
//将元素添加到dox中
$singleBook->documentElement->appendChild($element);
//输出结果(如果需要,请不要忘记保存:)
echo$singleBook->saveXML();
}
}
据我所知,您希望提取一个xml元素并将其另存为一个xml文件
如果是这样的话,就用这个
$xmlsassigndoc=newDOMDocument();
$xmlsassigndoc->load(“book.xml”);
$books=$xmlsassigndoc->getElementsByTagName(“项目”);
$singleBook=新的DOMDocument('1.0','utf-8');
$singleBook->formatOutput=true;
foreach($books作为$book){
$bookID=$book->getAttribute(“id”);
//下面是GET函数的位置
如果($bookID='483'){
$singleBook->loadXML(“”);
$node=$singleBook->importNode($book,true);
$singleBook->documentElement->appendChild($node);
echo$singleBook->saveXML();//我们需要保存吗?!?!
}
}
结果:
数据库系统管理和设计1
0877091153
http://library.hud.ac.uk/catlink/bib/483
28
CC140
或者,如果您只需要id作为结果
if($bookID==“483”){
$singleBook->loadXML(“”);
$node=$singleBook->createElement(“id”,$bookID);//documentElement->appendChild($node);
echo$singleBook->saveXML();
}
结果
483
在您的情况下,您希望输出是
用这个
$xmlsassigndoc=newDOMDocument();
$xmlsassigndoc->load(“books.xml”);
$books=$xmlsassigndoc->getElementsByTagName(“项目”);
$singleBook=新的DOMDocument('1.0','utf-8');
$singleBook->formatOutput=true;
foreach($books作为$book)
{
$bookID=$book->getAttribute(“id”);
//下面是GET函数的位置
如果($bookID='483'){
$singleBook->loadXML(“”);
$element=$singleBook->createElement(“book”);
//id属性
$attrId=$singleBook->createAttribute('id');
$attrId->value=$bookID;
//标题属性
$title=$book->getElementsByTagName(“title”);
$title=$title->item(0)->nodeValue;
$attrTitle=$singleBook->createAttribute('title');
$attrtTitle->value=$title;
//其余的属性
//将属性添加到元素中
$element->appendChild($attrId);
$element->appendChild($attrTitle);
//将元素添加到dox中
$singleBook->documentElement->appendChild($element);
//输出结果(如果需要,请不要忘记保存:)
echo$singleBook->saveXML();
}
}
您能否澄清代码中的哪些内容不起作用或给您带来问题?这是books.xml
?xml是book.xml中的唯一条目,我需要的是在php中,它以html格式输出该文件,但我无法以xml格式加载该文件。您能否澄清代码中的哪些内容不起作用或给您带来问题?这是books.xml
?xml是book.xml中的唯一条目,我需要的是在php中,它以html格式输出该文件,但我无法以xml格式加载该文件?问题是我需要将数据输出为xml文件?哦,我想是另一种方式…对不起,不用担心,输出需要在xml p中像这样