2015-09-07 93 views
1

我有这个文本文件(namelist.txt)生成XML列文本文件的:使用json_decode

{"0":"Mr Tony Test","1":"Ms Tina Testy"}

我尝试将其转换成XML:

<?php 
$xml = new DOMDocument(); 
$names = json_decode(file_get_contents('namelist.txt')); 
foreach ($names as $name) 
{ 
    $xml_name = $xml->createElement($name); 

} 
$xml->save("rss.xml"); 
?> 

我得到以下错误:

Fatal error: Uncaught exception 'DOMException' with message 'Invalid Character Error' in C:\xampp\htdocs\xibo\rss.php:6 Stack trace: #0 C:\xampp\htdocs\xibo\rss.php(6): DOMDocument->createElement('Mr Tony Te...') #1 {main} thrown in C:\xampp\htdocs\xibo\rss.php on line 6

这样甚至可能吗?

编辑1:

试图通过@spiky一个解决方案,但我得到了一个空白页面结果:

<?php 
$obj=('namelist.txt'); 
function json_to_xml($obj){ 
    $str = ""; 
    if(is_null($obj)) 
    return "<null/>"; 
    elseif(is_array($obj)) { 
     //a list is a hash with 'simple' incremental keys 
    $is_list = array_keys($obj) == array_keys(array_values($obj)); 
    if(!$is_list) { 
     $str.= "<hash>"; 
     foreach($obj as $k=>$v) 
     $str.="<item key=\"$k\">".json_to_xml($v)."</item>".CRLF; 
     $str .= "</hash>"; 
    } else { 
     $str.= "<list>"; 
     foreach($obj as $v) 
     $str.="<item>".json_to_xml($v)."</item>".CRLF; 
     $str .= "</list>"; 
    } 
    return $str; 
    } elseif(is_string($obj)) { 
    return htmlspecialchars($obj) != $obj ? "<![CDATA[$obj]]>" : $obj; 
    } elseif(is_scalar($obj)) 
    return $obj; 
    else 
    throw new Exception("Unsupported type $obj"); 
} 
?> 
+0

去走遍:http://stackoverflow.com/questions/856833/is-there-some-way-to-convert-json-to-xml-in- php – rahul

+0

显然我无法在我的环境中安装XML_Serializer。我通过@spiky尝试了答案,但我得到了一个空白页面。 – BlueFox

回答

1

如果你解码JSON是具有两个属性(01对象)。

var_dump(json_decode('{"0":"Mr Tony Test","1":"Ms Tina Testy"}')); 

输出:

object(stdClass)#1 (2) { 
    ["0"]=> 
    string(12) "Mr Tony Test" 
    ["1"]=> 
    string(13) "Ms Tina Testy" 
} 

你重复的属性和使用的值作为元素名称。但他们不是有效的名字。这是一个静态的例子产生错误:

$document = new DOMDocument(); 
$document->createElement('name with spaces'); 

输出:

Fatal error: Uncaught exception 'DOMException' with message 
'Invalid Character Error' in /tmp/... 

所以你要确保你生成一个有效的XML。喜欢这样的:

$json = json_decode('{"0":"Mr Tony Test","1":"Ms Tina Testy"}'); 

$document = new DOMDocument(); 
$names = $document->appendChild(
    $document->createElement('names') 
); 
foreach ($json as $value) { 
    $names 
    ->appendChild($document->createElement('name')) 
    ->appendChild($document->createTextNode($value)); 
} 

$document->formatOutput = TRUE; 
echo $document->saveXml(); 

输出:

<?xml version="1.0"?> 
<names> 
    <name>Mr Tony Test</name> 
    <name>Ms Tina Testy</name> 
</names> 

这是一个更好的主意,用一个自定义的节点结构的XML,而不是一个由数据定义。

您将结果命名为xml文件'rss.xml'。 RSS是一种定义的格式。所以如果你想生成RSS你必须生成特定的节点。

$json = json_decode('{"0":"Mr Tony Test","1":"Ms Tina Testy"}'); 

$document = new DOMDocument(); 
$rss = $document->appendChild($document->createElement('rss')); 
$rss->setAttribute('version', '2.0'); 
$channel = $rss->appendChild($document->createElement('channel')); 
foreach ($json as $value) { 
    $item = $channel->appendChild($document->createElement('item')); 
    $item 
    ->appendChild($document->createElement('title')) 
    ->appendChild($document->createTextNode($value)); 
} 

$document->formatOutput = TRUE; 
echo $document->saveXml(); 

输出:

<?xml version="1.0"?> 
<rss version="2.0"> 
    <channel> 
    <item> 
     <title>Mr Tony Test</title> 
    </item> 
    <item> 
     <title>Ms Tina Testy</title> 
    </item> 
    </channel> 
</rss> 
+0

感谢您的详细解答!完美的作品:-) – BlueFox