2010-02-10 74 views
7

我正在研究一些PHP,使用DOM擴展從數據庫創建XML。PHP DOM XML - 創建多個名稱空間屬性?

基本上,我需要創建一個命名空間,並添加3個屬性給它:

<NameSpaceName xmlns="uri:xxx" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation="uri:xxx"> 

完整的代碼我已經寫了低於:

include_once("includes/connect.php"); 

$sql = ("SELECT * FROM tableName"); 
$query = mysql_query($sql) or die("Error: " . mysql_error()); 


// create a new XML document 
$doc = new DomDocument('1.0', 'UTF-8'); 

// create root node 
$root = $doc->createElementNS('uri:xxx', 'PayerRecords'); 
$root = $doc->appendChild($root); 
$root->setAttributeNS('http://www.w3.org/2000/xmlns/' ,'xmlns:xsi', 'http://www.w3.org/2001/XMLSchema-instance'); 
$root->setAttributeNS('http://www.w3.org/2000/xmlns/' ,'xsi:schemaLocation', 'uri:xxx'); 

// process one row at a time 
while($row = mysql_fetch_assoc($query)) { 

    // add node for each row 
    $occ = $doc->createElement('Content'); 
    $occ = $root->appendChild($occ); 

    // add a child node for each field 
    foreach ($row as $fieldname => $fieldvalue) { 

    $child = $doc->createElement($fieldname); 
    $child = $occ->appendChild($child); 

    $value = $doc->createTextNode($fieldvalue); 
    $value = $child->appendChild($value); 

    } // foreach 

} // while 

// get completed xml document 
$xml_string = $doc->saveXML(); 

echo $xml_string; 

但是,當我執行上面的我得到這個錯誤:

Fatal error: Uncaught exception 'DOMException' with message 'Namespace Error' in xml.php:21 Stack trace: #0 xml.php(21): DOMElement->setAttributeNS(' http://www.w3.o ...', 'xsi:schemaLocat...', 'uri:xxx...') #1 {main} thrown in xml.php on line 21

21行是第二個'setAttributeNS'行。

任何人都可以看到我要去哪裏錯了嗎?

回答

15

的schemaLocation未在命名空間http://www.w3.org/2000/xmlns/說明,但http://www.w3.org/2001/XMLSchema-instance

<?php 
// create a new XML document 
$doc = new DomDocument('1.0', 'UTF-8'); 
// create root node 
$root = $doc->createElementNS('http://xxx', 'PayerRecords'); 
$root = $doc->appendChild($root); 
$root->setAttributeNS('http://www.w3.org/2000/xmlns/' ,'xmlns:xsi', 'http://www.w3.org/2001/XMLSchema-instance'); 
$root->setAttributeNS('http://www.w3.org/2001/XMLSchema-instance', 'schemaLocation', 'http://xxx'); 

echo $doc->savexml(); 

打印

<?xml version="1.0" encoding="UTF-8"?> 
<PayerRecords xmlns="http://xxx" 
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
       xsi:schemaLocation="http://xxx"/> 
2

取代線21

$root->setAttributeNS(
    'http://www.w3.org/2001/XMLSchema-instance', 
    'xsi:schemaLocation', 
    'http://xxx http://xxx/xxx.xsd' 
); 

xsi:schemaLocationhttp://www.w3.org/2000/xmlns/或命名空間沒有定義,但在xsi。所以你必須使用(完整的)xsi命名空間uri作爲第一個參數。

和:您不需要撥打setAttributeNS()兩次:上面的單行生成xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://xxx http://xxx/xxx.xsd"屬性。

3

我並沒有完全得到它的第一次,所以我張貼我的答案更詳細。也許有人認爲這有幫助。

// create DOM document 
$xml = new DomDocument('1.0', 'UTF-8'); 

// create root element 
$el = $xml->createElementNS('http://namespaceA/url/here/', 'rootelement'); 

// to be able to add new namespaces we must first add namespace 'xsi' 
// third parameter is important (use your main namespace with .xsd) 
$root->setAttributeNS(
    'http://www.w3.org/2001/XMLSchema-instance', 
    'xsi:schemaLocation', 
    'http://namespaceA/url/here/ http://namespaceA/xsdfile/here.xsd'); 

// add new namespace 
$el->setAttributeNS(
    'http://www.w3.org/2000/xmlns/', 
    'xmlns:namespaceB', 
    'http://namespaceB/url/here/'); 

// add root element to DOM 
$xml->appendChild($el); 

此郵件存檔消息是非常有用的:http://www.mail-archive.com/[email protected]/msg135362.html

相關問題