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 有没有会使用flac3d软件的家人
  • ¥20 360摄像头无法解绑使用,请教解绑当前账号绑定问题,
  • ¥15 docker实践项目
  • ¥15 数电几道习题,写出作答过程,ai一律不采用
  • ¥15 利用pthon计算薄膜结构的光导纳
  • ¥15 海康hlss视频流怎么播放
  • ¥15 Paddleocr:out of memory error on GPU
  • ¥30 51单片机C语言数码管驱动单片机为AT89C52
  • ¥100 只改动本课件的 cal_portfolio_weight_series(decision_date), 跑完本课件。设计一个信息比率尽量高的策略。
  • ¥20 如何在visual studio 2022中添加ImageMagick库