-2

我正在尝试实现从 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 方面更糟糕,任何有助于朝着正确方向发展的帮助都会很棒!

4

1 回答 1

1

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

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

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

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

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

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

单曲=也是一项任务,而不是比较,我敢肯定这不是你的意图。

$name$body查找是相同的:

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

同样,您忽略了$issue已枚举并正在从父节点工作的 ,然后只获取与issue_name和匹配的所有子元素issue_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;                                                                  
    }                                                                       
}

您的其余代码有更多相同的问题,但希望这将为您指明正确的方向。

于 2013-06-25T22:20:31.953 回答