convert pipe separated text file data to xml format in php

Home / Uncategorized / convert pipe separated text file data to xml format in php

Question:
I have a text file generated by a device which provides me the data in this form:Rajeet Singh|email@gmail.com|0123456789|Delhi
Abhishek|email2@gmail.com|0123456987|Pune
I want to extract the data from the text file and convert it into XML form as shown below:

Rajeet
email@gmail.com
0123654789

Delhi



Abhishek
email@gmail.com
0123654789

Pune



This is what I have done so far.

I have extracted the data from the text file and converted into an array.
$fh=fopen(‘file/dat.txt’,’r’);
$xml = new SimpleXMLElement(‘‘);

while($line=fgets($fh)){
$line_arr=explode(‘|’,$line);
}
And converted a static valued array to xml like this:$users_array = array(
array(
"name" => "Rajeet",
"email" => "email@gmail.com",
"mobile" => "0123654789",
"address" => "Delhi",
),
array(
"name" => "Abhishek",
"email" => "email@gmail.com",
"mobile" => "0123654789",
"address" => "Pune",
)
);
print_r($users_array);
//function defination to convert array to xml
function array_to_xml($array, &$xml_user_info) {
foreach($array as $key => $value) {
if(is_array($value)) {
if(!is_numeric($key)){
$subnode = $xml_user_info->addChild("$key");
array_to_xml($value, $subnode);
}else{
$subnode = $xml_user_info->addChild("item$key");
array_to_xml($value, $subnode);
}
}else {
$xml_user_info->addChild("$key",htmlspecialchars("$value"));
}
}
}

//creating object of SimpleXMLElement
$xml_user_info = new SimpleXMLElement("");

//function call to convert array to xml
array_to_xml($users_array,$xml_user_info);

//saving generated xml file
$xml_file = $xml_user_info->asXML(‘users.xml’);

//success and error message based on xml creation
if($xml_file){
echo ‘XML file have been generated successfully.’;
}else{
echo ‘XML file generation error.’;
}
?>
But I am not sure how to format the array I got from the Text file.
Any help will be highly appreciated. Thanks


Answer:
I am adding an XMLWriter solution as an alternative.

XmlWriter can be a faster and less memory intensive solution over DOM or SimpleXml because it uses streams instead of holding the entire xml tree in memory.

Like mentioned elsewhere, you don’t want , but instead use an ID attribute. XML provides this via the xml:id attribute. The code below makes use of this.

I am using an SplFileObject because it is a convenient file wrapper implementing the Iterator interface and providing CSV control.
setFlags(
SplFileObject::READ_CSV |
SplFileObject::DROP_NEW_LINE |
SplFileObject::READ_AHEAD |
SplFileObject::SKIP_EMPTY
);
$reader->setCsvControl(‘|’);

$writer = new XMLWriter;
$writer->openURI(‘file/dat.xml’); // write to this file
$writer->setIndent(true); // format the output
$writer->startDocument(‘1.0’, ‘UTF-8’);
$writer->startElement("user"); // the root element

foreach ($reader as $row_data) {
$row = array_map(‘trim’, $row_data);
$writer->startElement(‘item’);
$writer->writeAttributeNS(‘xml’, ‘id’, null, $reader->key());
$writer->writeElement("name", $row_data[0] ?? null);
$writer->writeElement("email", $row_data[1] ?? null);
$writer->writeElement("mobile", $row_data[2] ?? null);
$writer->writeElement("address", $row_data[3] ?? null);
$writer->endElement();
$writer->flush();
}

$writer->fullEndElement();
$writer->endDocument();

Note that this requires PHP7 due to the use of the null coalesce operator when writing the elements. If you are not on 7 yet, either update or use the old isset/empty constructs.
Read more

Leave a Reply

Your email address will not be published. Required fields are marked *