0

How can I do the following edits to an XML file in PHP. I basically want to keep some elements and write those elements to a new XML file.

So, I know how to open the new file and prepare it for writing and then open the XML file and iterate through it line by line:

$lines = fopen("file.xml", "r");
$new = fopen("newFile.xml", "w");
foreach($lines as $line){

    /* operations on each line here */
}

I don't want to do operations on each line, but on certain elements in the file.xml.

What I need to do is for each <doc> element (everything in between <doc> and </doc>):

  1. echo "<doc>" and break to a new line in $new.
  2. write everything in between <title> and </title> including the tags to $new.
  3. write everything in between <url> and </url> including the tags to $new.
  4. write everything in between <abstract> and <abstract> including the tags to $new.
  5. echo "</doc>" and break to a new line.

and then move on to the next <doc> </doc> block.

I would greatly appreciate all and any help in learning how to do the above.

Django Johnson
  • 1,383
  • 3
  • 21
  • 40

2 Answers2

6

Try doing something with the simplexml library like in the following example:

$xml = simplexml_load_file("file.xml"); //load file as SimpleXML object
$newXml = SimpleXMLElement(); // create new SimpleXML object

foreach ($xml->doc as $d){ // change "$xml->doc" to the path to doc in your file
    $doc = $newXml->addChild("doc"); // add <doc></doc>
    $doc->addChild("title", (string)$d->title); //add title child within doc
    $doc->addChild("url", (string)$d->url); //add url child within doc
    $doc->addChild("abstract", (string)$d->abstract); //add abstract child within doc
}
$new = fopen("newFile.xml", "w"); // open new file
fwrite($new, $newXml->asXML()); //write XML to new file using asXML method
fclose($new); // close the new file

Hope this helps. You can find the full documentation of simplexml here: http://php.net/simplexml.examples-basic and there are many more concrete questions and answers here on Stackoverflow:

hakre
  • 193,403
  • 52
  • 435
  • 836
Les
  • 76
  • 4
  • +1: Short and sound example. I just put some wording around so it's not only the code and leaving some references to the manual. – hakre Jun 26 '13 at 23:52
  • Thank you! This helps a lot! One thing that is confusing me is the comment `// change "$xml->doc" to the path to doc in your file`. Why would I change `$xml->doc`? Right now isn't it getting all `` elements in $xml or file.xml ? Since this script is in the same dir as file.xml, I shouldn't have to change anything, right? – Django Johnson Jun 27 '13 at 13:46
  • Hi Django, I added that comment in case `doc` was a child of something else in your XML file. For example, if your XML looked like `...`, you would have to change `$xml->doc` to `$xml->something->doc`. If `$xml->doc` is correctly getting `` elements, don't change anything. – Les Jun 27 '13 at 16:28
  • Testing it, simplexml_load_file fails unless file.xml has `` on firts line and a root element surrounding all. I've named `......` – Siot Jun 28 '13 at 07:40
  • @Les Hello. All, I see. No, all the `` elements are children of a ` element. It doesn't have a doctype / `` so the file loading fails. Also there isn't a `` element surrounding everything. There is a `` element surrounding everything. And I can't edit the file. – Django Johnson Jun 28 '13 at 13:55
  • @Siot Yes it does fail. Because there isn't a `` at the beginning of the file. There also isn't a `` element containing everything, but there is a ` element containing everything. The problem is that I can not edit file.xml, I can view it and parse it though. – Django Johnson Jun 28 '13 at 13:56
  • @DjangoJhonson well `` tag as `.........`can be enough. You can add tags to it loading file as string with `$file = "". file_get_contents('file.xml'); $xml = simplexml_load_string($file);` – Siot Jun 29 '13 at 13:58
  • Alert with the use of SimpleXML constructor [PHP reference](http://www.php.net/manual/en/simplexmlelement.construct.php) – Siot Jun 29 '13 at 13:59
1

In PHP you have two easy options for working with XML files. (There are others)

The first has already been pointed out: SimpleXML

The second option is DOM - Document Object Model

To make life even easier, you can search an XML file with XPath

$doc = new DOMDocument;    
$doc->load('myXMLFile.xml');

$xPath = new DOMXPath($doc);

// Give me all doc elements inside the XML
$docElements = $xPath->query('doc');

foreach ($docElements as $docElement) {
    // Work your magic here!
}
ManuelH
  • 846
  • 1
  • 15
  • 25