PHP DOM XML

编程入门 行业动态 更新时间:2024-10-26 10:30:02
本文介绍了PHP DOM XML - 创建多个命名空间属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在使用一些PHP从使用DOM扩展的数据库创建XML。

I'm working on some PHP to create XML from a database using the DOM extension.

基本上,我需要创建一个NameSpace并添加3个属性:

Basically, I need to create a NameSpace and add 3 attributes to it:

<NameSpaceName xmlns="uri:xxx" xmlns:xsi="www.w3/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('www.w3/2000/xmlns/' ,'xmlns:xsi', 'www.w3/2001/XMLSchema-instance'); $root->setAttributeNS('www.w3/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;

但是当我执行上面我得到这个错误:

But when I execute the above I get this error:

致命错误:未捕获的异常'DOMException'与$'中的消息'命名空间错误$ b xml.php:21 堆栈跟踪:#0 xml.php(21): DOMElement-> setAttributeNS(' http:// www .w3.o ...','xsi:schemaLocat ...','uri:xxx ...')#1 {main}在中抛出第21行的xml.php

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

第21行是第二个setAttributeNS行。

Line 21 is the second 'setAttributeNS' line.

任何人都可以看到我在哪里出错?

Can anyone see where i'm going wrong?

推荐答案

schemaLocation未在命名空间中声明 www.w3/2000/xmlns/ ,但在 www.w3/2001/XMLSchema-instance

schemaLocation is not declared in the namespace www.w3/2000/xmlns/ but in www.w3/2001/XMLSchema-instance

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

打印

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

更多推荐

PHP DOM XML

本文发布于:2023-11-05 19:22:25,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1561639.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:PHP   DOM   XML

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!