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>
):
echo "<doc>"
and break to a new line in $new.- write everything in between
<title>
and</title>
including the tags to $new. - write everything in between
<url>
and</url>
including the tags to $new. - write everything in between
<abstract>
and<abstract>
including the tags to $new. 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.