2013-02-27 75 views
2

下面是XML文件的內容:使用simpleXML解析嵌套命名空間的XML?

<?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 

這裏是我開始了代碼,但我不知道它是否是我下面還是有一些更好的方式來實現這一目標的正確的方法?

// 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; 

如何通過元素循環以獲取<w:t>的內容?

回答

4

Xpath的將可能是最簡單的:

// 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"; 
} 
+0

能否請您進一步闡述,如何讓有文本的每個節點的位置或路徑,以及我們將如何修改任何特定節點的文本? – atif 2013-02-27 10:25:45

+0

@atif:這超出了你的初始問題的範圍,並且還需要額外的信息,例如爲什麼你需要節點的完整路徑以及它將用於什麼,以及修改節點的意圖是什麼 - 即。是否適合內存使用,你是否要將文件寫回磁盤等。請發佈一個新的問題,並附上任何相關的代碼。 – prodigitalson 2013-02-27 13:58:16