2010-02-10 40 views
7

Tôi đang làm việc trên một số PHP để tạo XML từ cơ sở dữ liệu bằng cách sử dụng phần mở rộng DOM.PHP DOM XML - Tạo nhiều thuộc tính không gian tên?

Về cơ bản, tôi cần phải tạo ra một NameSpace và thêm 3 thuộc tính với nó:

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

Mã đầy đủ i đã viết là dưới đây:

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; 

Nhưng khi tôi thực hiện I nêu trên gặp lỗi này:

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

Dòng 21 là dòng 'setAttributeNS' thứ hai.

Có ai có thể thấy tôi đang đi đâu không?

Trả lời

15

schemaLocation không được khai báo trong namespace http://www.w3.org/2000/xmlns/ nhưng trong 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(); 

in

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

thay thế dòng 21 với

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

xsi:schemaLocation không được định nghĩa trong http://www.w3.org/2000/xmlns/ hoặc namespace của bạn, nhưng trong xsi. do đó bạn phải sử dụng uri không gian tên (đầy đủ) xsi làm thông số đầu tiên.

và: bạn không cần phải gọi setAttributeNS() hai lần: dòng đơn ở trên tạo cả thuộc tính xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://xxx http://xxx/xxx.xsd".

3

Tôi không hoàn toàn nhận được nó lần đầu tiên vì vậy tôi đăng câu trả lời của tôi một cách chi tiết hơn . Có thể ai đó thấy điều này hữu ích.

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

Thư lưu trữ thư này rất hữu ích: http://www.mail-archive.com/[email protected]/msg135362.html.

Các vấn đề liên quan