2013-06-25 39 views
-2

我试图实现从PHP供电的Web应用程序编辑基于XML的新闻馈送的功能。然而,它似乎并没有拯救。在PHP中使用DOM编辑子节点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糟糕的是,任何帮助,得到这个朝着正确的方向将是巨大的!

+0

这里的网站是专业的爱好者程序员。你可能想尝试一些其他论坛。当你要求“走向正确的方向”时。 – hakre

+0

嗯,我是CompSci专业,第一次只负责XML操作,所以我对这个主题完全陌生。经过几个小时的搜索和研究,我想我会寻求一些帮助,因为,你知道,这就是这个网站的用途。认为这将是值得一试。虽然我不知道我会遇到精英分子。 – ChrisDevWard

+0

@ChrisDevWard我个人并不认为你的问题有什么特别的错误,如果我的答案是作为精英主义者出现的,我很抱歉。我试图帮助你。 –

回答

1

在操作DOM的代码中存在各种各样的问题。只要看看for循环的内容,在开始使用此:

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

在上面的线,你已经采取了您在for循环中列举的$issue,然后引用它的父,这是相同的每一个问题,从而使枚举无关紧要。

你然后让所有issue_id元素在树中,与你这样做:

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

在这里,您使用的是$issue_id作为一个指标,即假设3的issue_id(例如)将永远是第三个问题,这可能是不正确的。

另外一个=是一个任务,而不是比较,我敢肯定这不是你的意图。

$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;                 
    }                  
} 

你的代码的其余部分有更多的同样的问题,但希望这将指向你在正确的方向。

+0

感谢您的回复。这是我第一个主要的PHP项目,所以我似乎犯了很多简单的错误,特别是在整个代码库连续数小时盯着(编辑XML元素是我在包装项目之前的最后一项任务之后)。感谢您的反馈,这有很多帮助! – ChrisDevWard

+1

@ChrisDevWard:如果您对代码审查感兴趣,可以在程序员网站上找到:http://programmers.stackexchange.com/。请保留关于主题的问题,为调试请求倾销大块代码块不是。 – hakre

+0

@hakre正式注意。 – ChrisDevWard