我试图实现从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糟糕的是,任何帮助,得到这个朝着正确的方向将是巨大的!
这里的网站是专业的爱好者程序员。你可能想尝试一些其他论坛。当你要求“走向正确的方向”时。 – hakre
嗯,我是CompSci专业,第一次只负责XML操作,所以我对这个主题完全陌生。经过几个小时的搜索和研究,我想我会寻求一些帮助,因为,你知道,这就是这个网站的用途。认为这将是值得一试。虽然我不知道我会遇到精英分子。 – ChrisDevWard
@ChrisDevWard我个人并不认为你的问题有什么特别的错误,如果我的答案是作为精英主义者出现的,我很抱歉。我试图帮助你。 –