将纯字符串转换为xml

时间:2012-09-24 17:35:02

标签: php xml string

我正在使用书籍网站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

3 个答案:

答案 0 :(得分: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);
}

现在您可以访问任意一行

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

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

答案 1 :(得分:1)

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

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

答案 2 :(得分: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>
*/
?>