在PHP中使用DOM编辑子节点XML

时间:2013-06-25 21:24:27

标签: php xml parsing dom xml-parsing

我正在尝试实现从PHP驱动的Web应用程序编辑基于XML的新闻源的功能。但是,它似乎永远不会保存。

我正在使用的XML文件是这样的:

<?xml version="1.0" standalone="yes"?>
<issues>                                                                    
        <issue>
            <issue_id>1</issue_id>
            <issue_name>Don't double my rates!</issue_name>
            <issue_body>Congress is on the verge of letting student rates double a week from today. Swing by the UC Lawn at 5:00 this Thursday to reach out to our Representatives and tell them: #DontDoubleMyRates!</issue_body></issue>
         <issue>
            <issue_id>2</issue_id>
            <issue_name>Proposed Senate Budget</issue_name>
            <issue_body>College Democrats are baffled by the proposed senate budget. This is our state, we must make our opinions heard! #NCGOPBudget #StopCuts</issue_body></issue>
         <issue>
            <issue_id>3</issue_id>
            <issue_name>Voter Suppression Law Invalidated!</issue_name>
            <issue_body>Join us in applauding the US Supreme Court for invalidating Arizona's voter-suppression law requiring that voters present proof of citizenship before voting!</issue_body></issue>
         <issue>
            <issue_id>4</issue_id>
            <issue_name>Here's an actual article I found interesting</issue_name>                    
            <issue_body>Actually, not really beacause I really didn't want to google for some arbitrary article to help test this out so here's a bunch of filler text to hopefully emulate at least the by-line of an article pertaining to the democratic party organization here on campus.</issue_body>
            </issue>
</issues>

以下是尝试编辑预先存在的节点的相关php脚本:

<?php                                                                           
$newName = $_POST['name'];                                                  
$newBody = $_POST['body'];                                                  
$issue_id = $_POST['edit'];                                                 

$dom = new DOMDocument;                                                     
$dom->preserveWhiteSpace = FALSE;                                           
$dom->load('issues.xml');                                                   

$xpath = new DOMXPath($dom);                                                
$query = '/issues/issue';                                          

foreach($xpath->query($query) as $issue) {                                  
    $id = $issue->parentNode->getElementsByTagName("issue_id");         
    if($id->item($issue_id)->nodeValue = $issue_id) {                   
    $name = $issue->parentNode->getElementsByTagName("issue_name");       
    $body = $issue->parentNode->getElementsByTagName("issue_body");       
    $name->item($issue_id-1)->nodeValue = '$newName';                           
    $body->item($issue_id-1)->nodeValue = '$newBody';                        
    break;                                                                  
    }                                                                       
}                                                                           

    $dom->save("issues.xml");                                                   
?>

这是引用页面,它遍历子节点,直到找到先前选择的节点的ID,然后在表格中显示它的信息。

<?php                                                                           
$issue_id = $_POST['edit'];                                                 
$issueArray = array(                                                        
'id' =>$_POST['id'],                                                        
'issue_name' => $_POST['issue_name'],                                       
'issue_body' => $_POST['issue_body'],                                       
);                                                                          

$dom = new DOMDocument;                                                     
$dom->preserveWhiteSpace = FALSE;                                           
$dom->load('issues.xml');                                                   

$xpath = new DOMXPath($dom);                                                
$query = '/issues/issue';                                          

$i = 0;                                                                     
echo "<body><form action='saveChanges.php' method='post'><table border='1'><tr><th>ID</th><th>Name</th><th>Body</th></tr>";

foreach($xpath->query($query) as $issue) {                                  
    $eventI = $issue->parentNode->getElementsByTagName("issue_id");         
    if($eventI->item($issue_id)->nodeValue = $issue_id) {                   
    $eventN = $issue->parentNode->getElementsByTagName("issue_name");       
    $eventP = $issue->parentNode->getElementsByTagName("issue_body");       
    print "<tr><td>'".$eventI->item($issue_id-1)->nodeValue."'></td><td>'".$eventN->item($issue_id-1)->nodeValue."'></td><td>'".$eventP->item($issue_id-1)->nodeValue."'</td></tr>";
    print "<tr><td></td><th>New Name</td><th>New Body</td></tr>";           
    print "<tr><td></td><td><input type='text' name='name'size='50'</input></td><td><input type='text' name='body' size='200'</input></td></tr>";
    print "<tr><td><input type='hidden' name='id' value='$issue_id'/></td><th><input type='submit' action='saveChanges.php' name='edit' method='post' value='Confirm Edit'/></th><th></th>";
    break;                                                                  
    }                                                                       
}                                                                           
print "</table></body>";                                                    
?>

我在PHP方面并不是那么出色,更糟糕的是在解析XML时,任何有助于实现这一目标的帮助都会很棒!

1 个答案:

答案 0 :(得分:1)

代码中存在各种操作DOM的问题。只需查看for循环的内容,就可以从这开始:

$id = $issue->parentNode->getElementsByTagName("issue_id");      

在上面的行中,您已经在for循环中获取了您枚举的$issue,然后引用了它的父项,这对于每个问题都是相同的,因此使枚举无关紧要。

然后,您将获得该树中的所有issue_id个元素,您可以使用这些元素执行此操作:

if($id->item($issue_id)->nodeValue = $issue_id) {                   

这里您使用$issue_id作为索引,假设issue_id为3(例如)总是第三个问题,这可能不是真的。

另外一个=是作业,而不是比较,我相信这不是你的意图。

$name$body查找相同:

$name = $issue->parentNode->getElementsByTagName("issue_name");       
$body = $issue->parentNode->getElementsByTagName("issue_body");       

您再次忽略已枚举的$issue并从父节点开始工作,然后只获取与issue_nameissue_body匹配的所有子元素。

你再次使用$issue_id作为索引:

$name->item($issue_id-1)->nodeValue = '$newName';                           
$body->item($issue_id-1)->nodeValue = '$newBody';                        

但这一次,你正在使用$issue_id-1 - 是否有原因?

此外,当您在php中使用单引号作为字符串时,不会扩展变量,因此名称将始终设置为文字字符串$newName,而不是该变量的值。你应该使用双引号,或者更好的是,只需直接赋值。

这更像我期望的代码:

foreach($xpath->query($query) as $issue) {        
    $id = $issue->getElementsByTagName("issue_id")->item(0);  
    if($id->nodeValue == $issue_id) {  
        $name = $issue->getElementsByTagName("issue_name")->item(0);       
        $body = $issue->getElementsByTagName("issue_body")->item(0);       
        $name->nodeValue = $newName;
        $body->nodeValue = $newBody;
        break;                                                                  
    }                                                                       
}

你的其余代码有更多相同的问题,但希望这会指出你正确的方向。