doupo2157 2016-07-02 11:17
浏览 48
已采纳

替换节点XMLDOM和PHP

I have a problem to replace an XML node while entering data into a form My code below :

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

I want to change the new values with the old values. The DOM documentations say : public DOMNode DOMNode::replaceChild ( DOMNode $newnode , DOMNode $oldnode )

and the form

                    <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  ?>" >&nbsp;&nbsp;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  ?>" >&nbsp;&nbsp;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  ?>" >&nbsp;&nbsp;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>

I have the message : Fatal Error: Call to undefined function replaceChild I've looked to How to update xml file using PHP? it seems that works for him.I did the same think...

Any idea? thanks!

  • 写回答

1条回答 默认 最新

  • doujia1163 2016-07-02 12:06
    关注

    $node = $xpath->query("/$racine/annonce[@id = '$annonce_no']"); does not give you a DOM node, it is a DOM node list. So you at least need $node = $xpath->query("/$racine/annonce[@id = '$annonce_no']")->item(0); to have an object that exposes a replaceChild method. However, the other variables assigned with e.g. $flagplus_new = $_POST['flagplus']; are not nodes at all you could pass to that method, nor are the variables $flagplus = $result->textContent; which are all string values. If you want to replace an element's content with a new value then selecting that element and setting the textContent seems like the right approach.

    So assuming e.g.

    $node = $xpath->query("/$racine/annonce[@id = '$annonce_no']")->item(0);
    

    gives you the right element then you can select the description child with e.g.

    $descEl = $xpath->query('description', $node)->item(0);
    

    and changes its contents with e.g.

    $descEl->textContent = $newDescription;
    

    The only drawback is that the documentation http://php.net/manual/en/class.domnode.php says "5.6.1 The textContent property has been made writable (formerly it has been readonly). ".

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog
  • ¥15 Excel发现不可读取的内容
  • ¥15 关于#stm32#的问题:CANOpen的PDO同步传输问题
  • ¥20 yolov5自定义Prune报错,如何解决?