2012-09-24 76 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> 
*/ 
?>