1

I have this Textfile (namelist.txt):

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

And I try to convert it into 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");
?>

I get the following Error:

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

Is is even possible like this?

Edit 1:

Tried a solution by @spiky but I get a blank page as result:

<?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");
}
?>
BlueFox
  • 161
  • 1
  • 2
  • 12
  • Go through: http://stackoverflow.com/questions/856833/is-there-some-way-to-convert-json-to-xml-in-php – rahul Sep 07 '15 at 12:51
  • Apparently I´am unable to intstall XML_Serializer in my enviroment. I tried the answer by @spiky but I get a blank page. – BlueFox Sep 07 '15 at 13:10

1 Answers1

1

If you decode the JSON is an object with two properties (0 and 1).

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

Output:

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

You iterate the properties and use the values as element names. But they aren't valid names. Here is a static example producing the error:

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

Output:

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

So you to make sure that you generate a valid XML. Likes this:

$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();

Output:

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

It is a better idea to use a definable node structure for the XML, not one that is defined by the data.

You named the result xml file 'rss.xml'. RSS is a defined format. So if you like to generate RSS you have to generate specific nodes.

$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();

Output:

<?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>
ThW
  • 19,120
  • 3
  • 22
  • 44