2

Below is the content of xml file:

 <?xml version="1.0" encoding="UTF-8" standalone="yes"?> <w:document xmlns:ve="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships" xmlns:m="http://schemas.openxmlformats.org/officeDocument/2006/math" xmlns:v="urn:schemas-microsoft-com:vml" xmlns:wp="http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing" xmlns:w10="urn:schemas-microsoft-com:office:word" xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main" xmlns:wne="http://schemas.microsoft.com/office/word/2006/wordml"> <w:body> <w:p w:rsidR="00546015" w:rsidRDefault="00546015"> <w:r> <w:t xml:space="preserve">Hello </w:t> </w:r> <w:proofErr w:type="spellStart"/> <w:r> <w:t>Doctor</w:t> </w:r> <w:proofErr w:type="spellEnd"/> <w:r> <w:t>,</w:t> </w:r> </w:p> <w:p w:rsidR="00546015" w:rsidRDefault="00546015" w:rsidP="00B72192"> <w:r> <w:t xml:space="preserve">I hope you are doing well. Thanks for taking the time to speak with us on Skype yesterday. It is always a pleasure talking with you. </w:t> </w:r> </w:p> <w:p w:rsidR="00546015" w:rsidRDefault="00546015"/> . . . . . and this list goes on 

Here is my start up code but am not sure whether it is the right approach I am following or there is some better way to achieve this?

// load the xml into the object $xml = simplexml_load_file('word/document.xml'); //Use that namespace $namespaces = $xml->getNameSpaces(true); //Now we don't have the URL hard-coded $w_doc = $xml->children($namespaces['w']); $document = $w_doc->document; $w_body = $document->document->children($namespaces['w']); $body = $w_body->body; 

How to loop through elements in order to get the content of <w:t>?

1 Answer 1

4

Xpath would probably be the easiest:

// load the xml into the object $xml = simplexml_load_file('word/document.xml'); //Use that namespace $namespaces = $xml->getNameSpaces(true); $xml->registerXPathNamespace('w', $namespaces['w']); $nodes = $xml->xpath('/w:document/w:body//w:t'); foreach($nodes as $node) { echo (string) $node . "\n\n"; } 
Sign up to request clarification or add additional context in comments.

2 Comments

Can you please further elaborate, how to get the position or path of each node that has text, and how will we modify the text of any specific node?
@atif: Thats out of scope for your inital question and would also need additional information like why you need the full path to the node and what it will be used for, and what the intention is of modifying a node - ie. is it jsut for in-memory use, are you going to write the file back to disk, etc. Please post an new question with those details plus any relevant code.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.