2012-09-24 71 views
0

我在书本网站biblio中使用API​​,输入参数和查询书籍后,我得到一个正常的字符串返回,出于某种原因,它们不以xml格式返回字符串,我的问题...有没有一种简单的方法来将纯字符串转换为PHP中的XML?将纯文本字符串转换为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 
+0

为什么你需要它是XML格式?如果要将其重新转换为文本,则没有意义 – Ibu

+0

在执行对API的调用时是否可以指定Accept类型?默认情况下,它可能以纯文本形式返回。 – tomasmcguinness

+0

@Ibu因为最好使用xml而不是preg_match –

回答

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; 

可以通过每劈裂它开始line

$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 

等访问苯胺。我希望这是你正在寻找

+0

第一次爆炸是好的,它把每一行放到一个数组中,但是for循环中必须有一个错误,因为$ result返回为空 –

+0

你可以在循环中做一个print_f($ line)来看看有什么问题 – Ibu

+0

你的意思是print_r()? –

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>"; 
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> 
*/ 
?>