在将数据输入表单时替换XML节点时遇到问题 我的代码如下:
$xml = new DOMDocument("1.0", "ISO-8859-1");
$xml->preserveWhiteSpace = FALSE;
$xml->formatOutput = TRUE;
$xml->load($fichier);
$xpath = new DOMXPATH($xml);
$query = $xpath->query("/$racine/annonce[@id = '$annonce_no']/flagplus");
foreach($query as $result){ $flagplus = $result->textContent;}
$query = $xpath->query("/$racine/annonce[@id = '$annonce_no']/entete");
foreach($query as $result){ $entete = utf8_encode($result->textContent);}
$query = $xpath->query("/$racine/annonce[@id = '$annonce_no']/nouveau");
foreach($query as $result){ $nouveau = $result->textContent;}
$query = $xpath->query("/$racine/annonce[@id = '$annonce_no']/description");
foreach($query as $result){ $description = utf8_encode($result->textContent);}
$query = $xpath->query("/$racine/annonce[@id = '$annonce_no']/couleur");
foreach($query as $result){ $couleur = $result->textContent;}
$query = $xpath->query("/$racine/annonce[@id = '$annonce_no']/detail");
foreach($query as $result){ $detail = utf8_encode($result->textContent);}
$detail = str_replace('<br/>', PHP_EOL, $detail);
$query = $xpath->query("/$racine/annonce[@id = '$annonce_no']/autre");
foreach($query as $result){ $autre = utf8_encode($result->textContent);}
$query = $xpath->query("/$racine/annonce[@id = '$annonce_no']/contact");
foreach($query as $result){ $contact = utf8_encode($result->textContent);}
$query = $xpath->query("/$racine/annonce[@id = '$annonce_no']/vente");
foreach($query as $result){ $vente = utf8_encode($result->textContent);}
$node = $xpath->query("/$racine/annonce[@id = '$annonce_no']");
if (isset($_POST['submitSave'])){
$id_new = $_POST['id'];
$flagplus_new = $_POST['flagplus'];
$entete_new = htmlentities($_POST['entete'], ENT_XML1);
$nouveau_new = $_POST['nouveau'];
$description_new = htmlentities($_POST['description'], ENT_XML1);
$couleur_new = $_POST['couleur'];
$detail_new = htmlentities($_POST['detail'], ENT_XML1);
$autre_new = htmlentities($_POST['autre'], ENT_XML1);
$contact_new = htmlentities($_POST['contact'], ENT_XML1);
$vente_new = htmlentities($_POST['vente'], ENT_XML1);
$node->replaceChild($id_new, $id);
$node->replaceChild($flagplus_new, $flagplus);
$node->replaceChild($entete_new, $entete);
$node->replaceChild($description_new, $description);
$node->replaceChild($couleur_new, $couleur);
$node->replaceChild($detail_new, $detail);
$node->replaceChild($autre_new, $autre);
$node->replaceChild($contact_new, $contact);
$node->replaceChild($vente_new, $vente);
$xml->save($fichier);
我想用旧值更改新值。 DOM文档说:public DOMNode DOMNode :: replaceChild(DOMNode $ newnode,DOMNode $ oldnode)
和表格
<form method="post" class="thumbnail">
<div>
<p>ID</br>
<input type="text" name="id" style="width:30px;" maxlength="3" value="<?php echo $annonce_no ?>" ></p>
</div>
<div>
<p>2ème drapeau</br>
<input type="text" name="flagplus" style="width:20px;" maxlength="2" value="<?php echo $flagplus ?>" > blanc,FR,GE,VD,VS</p>
</div>
<div>
<p>Nouveau</br>
<input type="text" name="nouveau" style="width:10px;" maxlength="1" value="<?php echo $nouveau ?>" > 0=Non, 1=Oui</p>
</div>
<div>
<p>Couleur description</br>
<input type="text" name="couleur" style="width:10px;" maxlength="1" value="<?php echo $couleur ?>" > blanc ou R = rouge</p>
</div>
<div>
<p>Entete</br>
<input type="text" name="entete" style="width:20%" value="<?php echo $entete ?>" ></p>
</div>
<div>
<p>Description</br>
<input type="text" name="description" style="width:80%" value="<?php echo $description ?>" ></p>
</div>
<div>
<p>Détail annonce</br>
<textarea name="detail" style="width:80%" cols="200" rows="8"><?=$detail;?></textarea></p>
</div>
<div>
<p>Autre</br>
<input type="text" name="autre" style="width:80%" value="<?php echo $autre ?>" ></p>
</div>
<div>
<p>Contact</br>
<input type="text" name="contact" style="width:80%" value="<?php echo $contact ?>" ></p>
</div>
<div>
<p>Vente</br>
<input type="text" name="vente" style="width:20%;" value="<?php echo $vente ?>" ></p>
</div>
<div align="center"><input type="submit" name="submitSave" value="Enregistrer" class="btn btn-primary" style="width:150px"></div>
</form>
我有消息:致命错误:调用未定义的函数replaceChild 我已经看过How to update xml file using PHP?它似乎对他有用。我做了同样的想法...
有什么想法吗?谢谢!
答案 0 :(得分:0)
$node = $xpath->query("/$racine/annonce[@id = '$annonce_no']");
没有给你一个DOM节点,它是一个DOM节点列表。因此,您至少需要$node = $xpath->query("/$racine/annonce[@id = '$annonce_no']")->item(0);
来拥有一个公开replaceChild
方法的对象。但是,其他变量分配给例如$flagplus_new = $_POST['flagplus'];
不是您可以传递给该方法的节点,也不是变量$flagplus = $result->textContent;
都是字符串值。如果您想用新值替换元素的内容,那么选择该元素并设置textContent
似乎是正确的方法。
所以假设例如。
$node = $xpath->query("/$racine/annonce[@id = '$annonce_no']")->item(0);
为您提供正确的元素,然后您可以选择description
子项,例如
$descEl = $xpath->query('description', $node)->item(0);
并用例如
更改其内容$descEl->textContent = $newDescription;
唯一的缺点是文档http://php.net/manual/en/class.domnode.php说“5.6.1 textContent属性已经可写(以前它只是readonly)。”。