PHP DOMdocument问题(字符,文件) [英] PHP DOMdocument problems (characters, file)

查看:113
本文介绍了PHP DOMdocument问题(字符,文件)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我通过创建和浏览DOM树,提取了您在页面左侧中看到的类别。页面的。现在,我想创建一个新的DOM,以将其存储在服务器上并在本地重新加载它,并加快整个过程。我决定在探索原始DOM时这样做。对原始DOM的探索是可行的,因此,假设参数正确。



我编写以下代码来创建DOM:

  $ curr_lev = 1; 
$ mydom = new DOMdocument();
$ curr_parent = $ mydom-> createElement(‘products’);
函数create_dom($ name,$ link,$ lev){
global $ curr_lev;
全球$ curr_parent;
全球$ mydom;
开关($ lev){
case $ curr_lev:
$ curr_parent-> appendChild($ mydom-> createElement($ name,$ link));
休息时间;
case $ curr_lev-1:
$ curr_parent = $ curr_padre-> parentNode;
$ curr_parent-> appendchild($ mydom-> createElemnt($ name,$ link));
休息时间;
case $ curr_lev + 1:
$ curr_parent = $ curr_padre-> lastChild;
$ curr_parent-> appendchild($ mydom-> createElement($ name,$ link));
休息时间;
}
$ curr_lev = $ lev;
}

$ mydom-> formatOutput = TRUE;
$ mydom-> saveHTMLFile( products.xml);

我尝试给出一个解释: create_dom()为原始DOM的每个节点调用它。 $ lev 表示新节点的级别, $ curr_lev 是最后添加的节点的级别,因此如果它们等于最后添加的节点,当前节点是同一父节点的子节点,如果 $ lev< $ curr_lev 我们必须返回上一级,如果 $ lev> ;,则新添加的节点是最后添加的父节点的兄弟。 $ curr_lev 当前节点是最后添加的节点的子节点。



第一个问题是,当我执行该操作时会遇到此错误:


致命错误:C:\Users\Jacopo\Dropbox\Tirocinio中出现消息 Invalid Character Error的未捕获异常 DOMException \xampp-portable\htdocs\站点\prova\cerca categorie.php:71

堆栈跟踪:

#0 C:\用户\Jacopo\ \Dropbox\Tirocinio\xampp-便携式\htdocs\网站\prova\cerca categorie.php(71):DOMDocument-> createElement('/ joomla / compone ...','Arduino')

#1 C:\Users\Jacopo\Dropbox\Tirocinio\xampp-portable\htdocs\sites\prova\cerca categorie.php(30):create_dom('Arduino' ,'/ joomla / compone ...',1)

#2 C:\Users\Jacopo\Dropbox\Tirocinio\xampp-portable\htdocs\sites\prova \cerca categorie.php(38):visita_raff(Object(DOMElement),1,'dl')

#3 C:\用户\Jacopo\Dropbox\Tirocinio\xampp-可移植的文档站点provacercer categorie.ph p(96):visita_raff(Object(DOMElement),0,``)

#4 C:\Users\Jacopo\Dropbox\Tirocinio\xampp-portable\htdocs\ sites\prova\index.php(21):include('C:\Users\Jacopo ...')#5 {main}抛出C:\Users\Jacopo\Dropbox\Tirocinio第71行上的xampp-portable\htdocs\sitesprovacacerca categorie.php


$ name 通常看起来像 arduino工具包,而 $ link 看起来像 / joomla / componenent / virtuamart /... p>

我尝试将其转换为UTF-8,但无法正常工作



我也尝试过测试并编写以下代码:

  function create_xml(){
$ mydom = new DOMdocument( 1.0, ISO-8859-1);
$ primoElem = $ mydom-> createElement(’foo’);
$ primoElem-> appendChild($ mydom-> createElement(’arduinio’,’http:arduino’));
$ mydom-> formatOutput = TRUE;
返回$ mydom-> saveXML( foo.xml);
}

我没有错误 saveXML()返回1,但没有写入文件!



我在做什么错?请考虑这是我第一次使用这些东西,所以要变得温柔:)

解决方案

异常 DOMException 并显示消息


无效字符错误


表示您已尝试创建元素( DOMDocument :: createElement() )的元素名称中包含无效字符:

  $ mydom-> createElement($ name,$ link)
^
|
第一个参数是元素名称

在XML中,并非每个名称都有效,有些甚至包含无效字符(例如,空格 或反斜杠 / )或无效的字节序列来自Unicode UTF-8范围。 PHP中的DOMDocument仅接受UTF-8作为输入。因此对于一般。如果您想深入了解XML元素名称中哪些字符有效,可以在 如何检查string是否是有效的XML元素名称?



因此,现在如果您仔细查看错误消息的堆栈跟踪,您甚至可能会发现问题:


  DOMDocument-> createElement('/ joomla / compone ...','Arduino')
^ ^


/ 字符在XML元素名称中无效。解决此问题,您应该可以只添加您的东西。只需使用最后一个有效的元素名称即可。


I've extracted the categories you see on the left of this page by creating and exploring the DOM tree of the page. Now I want to create a new DOM to store it on my server and reload it locally and speed-up the whole process. I decided to do that while exploring the original DOM. The exploration of the original DOM works, so assume that the parameters are correct.

I write this code to create the DOM:

$curr_lev=1;
$mydom=new DOMdocument();
$curr_parent=$mydom->createElement('products');
function create_dom($name, $link, $lev){
    global $curr_lev;
    global $curr_parent;
    global $mydom;
    switch ($lev){
        case $curr_lev:
            $curr_parent->appendChild($mydom->createElement($name, $link));
            break;
        case $curr_lev-1:
            $curr_parent=$curr_padre->parentNode;
            $curr_parent->appendchild($mydom->createElemnt($name, $link));
            break;
        case $curr_lev+1:
            $curr_parent=$curr_padre->lastChild;
            $curr_parent->appendchild($mydom->createElement($name, $link));
            break;   
    }
    $curr_lev=$lev;
}

$mydom->formatOutput=TRUE;
$mydom->saveHTMLFile("products.xml");

i try to give an explanation: create_dom() it's called for each node of the original DOM. $lev indicates the level of the new node, $curr_lev it's the level of the last added node, so if they are equal last node added and the current node are child of the same father, if $lev < $curr_lev we have to go back of one level and the new added node is "brother" of the father of the last added, if $lev > $curr_lev the current node is child of the last node added.

The first problem is that when I execute I get this error:

Fatal error: Uncaught exception 'DOMException' with message 'Invalid Character Error' in C:\Users\Jacopo\Dropbox\Tirocinio\xampp-portable\htdocs\sites\prova\cerca categorie.php:71
Stack trace:
#0 C:\Users\Jacopo\Dropbox\Tirocinio\xampp-portable\htdocs\sites\prova\cerca categorie.php(71): DOMDocument->createElement('/joomla/compone...', 'Arduino')
#1 C:\Users\Jacopo\Dropbox\Tirocinio\xampp-portable\htdocs\sites\prova\cerca categorie.php(30): create_dom('Arduino', '/joomla/compone...', 1)
#2 C:\Users\Jacopo\Dropbox\Tirocinio\xampp-portable\htdocs\sites\prova\cerca categorie.php(38): visita_raff(Object(DOMElement), 1, 'dl')
#3 C:\Users\Jacopo\Dropbox\Tirocinio\xampp-portable\htdocs\sites\prova\cerca categorie.php(96): visita_raff(Object(DOMElement), 0, '')
#4 C:\Users\Jacopo\Dropbox\Tirocinio\xampp-portable\htdocs\sites\prova\index.php(21): include('C:\Users\Jacopo...') #5 {main} thrown in C:\Users\Jacopo\Dropbox\Tirocinio\xampp-portable\htdocs\sites\prova\cerca categorie.php on line 71

$name usually look like "arduino kit" and $link is like "/joomla/componenent/virtuamart/..."

I've tried converting it to UTF-8 but it wont work

Also I've tried to do a test and write this code:

function create_xml(){
    $mydom=new DOMdocument("1.0", "ISO-8859-1");
    $primoElem=$mydom->createElement('foo');
    $primoElem->appendChild($mydom->createElement('arduinio', 'http:arduino'));
    $mydom->formatOutput=TRUE;
    return $mydom->saveXML("foo.xml");
}

I get no error saveXML() returns 1, but nothing is written to the file!

What am I doing wrong? Please consider that is the first time I work with those things so be gentle :)

解决方案

The Exception DOMException with the message

Invalid Character Error

means that you have tried to create an element (DOMDocument::createElement()) containing invalid characters in the element name:

$mydom->createElement($name, $link)
                        ^
                        |
           first parameter is the element name

In XML not every name is valid, some even contain invalid characters (for example a space " " or the backslash /) or invalid byte-sequences that aren't anything from the Unicode UTF-8 range. DOMDocument in PHP accepts UTF-8 as input only. So for for general. If you want to learn in depth which characters are valid in XML element names you can find more information that you will likely ever need in your live in How to check if string is a valid XML element name?.

So for now if you look closely to the stacktrace of the error message you can probably even spot the problem:

DOMDocument->createElement('/joomla/compone...', 'Arduino') 
                            ^      ^

The / character is not valid inside an XML element name. Fix the issue and you should be able to just add your stuff. Just use an element name that is valid in the end.

这篇关于PHP DOMdocument问题(字符,文件)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆