Reputation: 2885
I was wondering if it is possible to open an xml file as a plain text file so I can read in each line and manipulate the text?
Upvotes: 0
Views: 158
Reputation: 32145
$xml_file = '/var/www/file.xml';
$contents = file_get_contents($xml_file); // Dumps the entire file into a single string
$contents = file($xml_file); // Dumps each line into an array
However, I would recommend using simplexml_load_file()
(even though you said you wanted to avoid it) because there is no guarantee as to how the xml will be formatted. It could all be on a single line or formatted with line-breaks in unexpected places.
Upvotes: 3
Reputation: 28713
To read file as array of strings use file
function:
$lines = file('your_xml_file.xml');
foreach($lines as $line) {
## do the stuff for each line
}
Upvotes: 0
Reputation: 43619
Why not use any of the XML parser/manipulator directly?
You can find those references at http://www.php.net/manual/en/refs.xml.php
If you have a nicely formatted XML file then,
$file = 'file.xml';
// get contents and normalize the newline
$xml_arr = file($file);
foreach($xml_arr as &$line){
// do your manipulation to $line
}
$ctns = implode("\n",$xml_arr);
file_put_contents($file,$ctns); // write back
Upvotes: 1