0

我正在使用图书网站 biblio 中的 API,在输入参数并查询一本书后,我得到了一个正常的字符串返回,由于某种原因,他们没有以 xml 格式返回字符串,我的问题......有没有简单的方法在 php 中将纯字符串转换为 xml?

纯字符串示例

Book ID: 524072799
Author: George R. R. Martin
Title: A Song of Ice and Fire, 7 Volumes 
Description: Harper Voyager, 2012. New and in stock.. Paperback. New. Book. 
ISBN: 9780007477159
Publisher: Harper Voyager
Date: 2012
Currency: GBP
Price: 55.00
Bookseller: El Pinarillo Books 
Bookseller_Location: GBR
URL: http://www.biblio.com/details.php?dcx=524072799
Shipping Ground: 3.15 
Shipping Expedited: 3.95
4

3 回答 3

1

这是另一种选择,您可以利用 php 数组(不需要 preg_match 或 XML):

// The string returned;
$string = <<<STRING
Book ID: 524072799
Author: George R. R. Martin
......
Shipping Ground: 3.15 
Shipping Expedited: 3.95
STRING;

您可以从每行拆分开始

$lines = explode("\n",$string);

然后将其保存为数组中的键对值

$result = array();
foreach($lines as $line){
   list($key,value) = explode(":",$line);
   $result[trim($key)] = trim($value);
}

现在你可以像这样访问anyline

echo $result['Book ID']; // 524072799
echo $result['Author']; //  George R. R. Martin

等等。我希望这就是你要找的

于 2012-09-24T17:47:43.383 回答
1

只需使用explode("\n",$text)andforeach()循环来写入文件:

$xml="<book>";
$rows = explode("\n",$text);
foreach($rows as $row){
   $pieces = explode(':',$row); //get the tag and value
   $tag = str_replace(' ','_',strtolower($pieces[0])); //make sure the tag is valid
   if (!isset($pieces[1])){$pieces[1]="";} //make sure there is a value
   $xml.="<$tag>".$pieces[1]."</$tag>\n";//add it to the xml
}
$xml.="</book>";
于 2012-09-24T17:48:28.590 回答
1

如果您确定没有选项可以取回 xml 或 json,您可以这样做。

从响应中创建一个数组:

<?php 
$str = 'Book ID: 524072799
Author: George R. R. Martin
Title: A Song of Ice and Fire, 7 Volumes 
Description: Harper Voyager, 2012. New and in stock.. Paperback. New. Book. 
ISBN: 9780007477159
Publisher: Harper Voyager
Date: 2012
Currency: GBP
Price: 55.00
Bookseller: El Pinarillo Books 
Bookseller_Location: GBR
URL: http://www.biblio.com/details.php?dcx=524072799
Shipping Ground: 3.15 
Shipping Expedited: 3.95
';

$lines = explode('<br />',nl2br($str));

$array = array();
foreach($lines as $line){
    $line = trim($line);
    if(empty($line)){continue;}
    $line_parts = explode(':',$line);
    $array[$line_parts[0]] = $line_parts[1];
}
/*
You now have Array
(
    [Book ID] =>  524072799
    [Author] =>  George R. R. Martin
    [Title] =>  A Song of Ice and Fire, 7 Volumes
    [Description] =>  Harper Voyager, 2012. New and in stock.. Paperback. New. Book.
    [ISBN] =>  9780007477159
    [Publisher] =>  Harper Voyager
    [Date] =>  2012
    [Currency] =>  GBP
    [Price] =>  55.00
    [Bookseller] =>  El Pinarillo Books
    [Bookseller_Location] =>  GBR
    [URL] =>  http
    [Shipping Ground] =>  3.15
    [Shipping Expedited] =>  3.95
)
*/
?>

然后从该数组创建 XML:

<?php
//To create and output xml from the given array
header('Content-Type: text/xml');
$xml = new SimpleXMLElement('<?xml version="1.0" encoding="UTF-8"?><books/>');

$node = $xml->addChild('book');
foreach($array as $key=>$value){
    $node->addChild(str_replace(' ','_',$key), trim($value));
}

//DOMDocument to format code output
$dom = new DOMDocument('1.0');
$dom->preserveWhiteSpace = false;
$dom->formatOutput = true;
$dom->loadXML($xml->asXML());

echo $dom->saveXML();
/*
<?xml version="1.0" encoding="UTF-8"?>
<books>
  <book>
    <Book_ID>524072799</Book_ID>
    <Author>George R. R. Martin</Author>
    <Title>A Song of Ice and Fire, 7 Volumes</Title>
    <Description>Harper Voyager, 2012. New and in stock.. Paperback. New. Book.</Description>
    <ISBN>9780007477159</ISBN>
    <Publisher>Harper Voyager</Publisher>
    <Date>2012</Date>
    <Currency>GBP</Currency>
    <Price>55.00</Price>
    <Bookseller>El Pinarillo Books</Bookseller>
    <Bookseller_Location>GBR</Bookseller_Location>
    <URL>http</URL>
    <Shipping_Ground>3.15</Shipping_Ground>
    <Shipping_Expedited>3.95</Shipping_Expedited>
  </book>
</books>
*/
?>
于 2012-09-24T17:57:06.310 回答