Home > other >  xPath problem while changing a XML file with PHP
xPath problem while changing a XML file with PHP

Time:04-03

I'm trying to modify a XML file. I tried same code with an more basic XML file and it worked perfectly. But when I use the same code for some other XML file with changing the path, I am getting this:

Notice: Indirect modification of overloaded element of DOMNodeList has no effect in /storage/ssd2/119/18719119/public_html/pages/create_config/index.php on line 19

Warning: Creating default object from empty value in /storage/ssd2/119/18719119/public_html/pages/create_config/index.php on line 19

Fatal error: Uncaught Error: Call to a member function appendChild() on null in /storage/ssd2/119/18719119/public_html/pages/create_config/index.php:25 Stack trace: #0 {main} thrown in /storage/ssd2/119/18719119/public_html/pages/create_config/index.php on line 25

This is the XML file: (I sign the line I want to change for example)

<?xml version="1.0" encoding="UTF-8"?>
<config xmlns="http://www.linphone.org/xsds/lpconfig.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.linphone.org/xsds/lpconfig.xsd lpconfig.xsd">
  <section name="sip">
    <entry name="default_proxy" overwrite="true">0</entry>
  </section>
  <section name="net">
    <entry name="nat_policy_ref" overwrite="true">~OuCpkaPzCwyvMo</entry>
  </section>
  <section name="misc">
    <entry name="transient_provisioning" overwrite="true">1</entry> 
  </section>
  <section name="nat_policy_default_values">
    <entry name="stun_server">stun.linphone.org</entry>
    <entry name="protocols">stun,ice</entry>
  </section>
  <section name="nat_policy_0">
    <entry name="ref" overwrite="true">~OuCpkaPzCwyvMo</entry>
    <entry name="stun_server" overwrite="true">stun.linphone.org</entry>
    <entry name="protocols" overwrite="true">stun,ice</entry>
  </section>
  <section name="auth_info_0" overwrite="true">
    <entry name="username" overwrite="true">bilbo.baggins</entry><!-- I WANT TO CHANGE THE "bilbo.baggins" TEXT IN HERE -->
    <entry name="ha1" overwrite="true">7f37aeccfad9855e4584c5465d6b4638</entry>
    <entry name="realm" overwrite="true">sip.linphone.org</entry>
    <entry name="domain" overwrite="true">sip.linphone.org</entry>
    <entry name="algorithm" overwrite="true">MD5</entry>
  </section>
  <section name="proxy_0" overwrite="true">
    <entry name="reg_proxy" overwrite="true">&lt;sip:sip.linphone.org;transport=tls&gt;</entry>
    <entry name="reg_route" overwrite="true">&lt;sip:sip.linphone.org;transport=tls&gt;</entry>
    <entry name="reg_identity" overwrite="true">"Bilbo Baggins" &lt;sip:[email protected]&gt;</entry>
    <entry name="realm" overwrite="true">sip.linphone.org</entry>
    <entry name="quality_reporting_collector" overwrite="true">sip:[email protected];transport=tls</entry>
    <entry name="quality_reporting_enabled" overwrite="true">1</entry>
    <entry name="quality_reporting_interval" overwrite="true">180</entry>
    <entry name="reg_expires" overwrite="true">31536000</entry>
    <entry name="reg_sendregister" overwrite="true">1</entry>
    <entry name="publish" overwrite="true">1</entry>
    <entry name="avpf" overwrite="true">1</entry>
    <entry name="avpf_rr_interval" overwrite="true">1</entry>
    <entry name="nat_policy_ref" overwrite="true">~OuCpkaPzCwyvMo</entry>
  </section>
</config>

So I use the code piece below:

<?php
            if(isset($_REQUEST['Create'])){
                $var = $_POST["name"];
                
                $xml = file_get_contents("remote_prov2isioning.xml");
                $dom = new DomDocument();
                $dom->loadXml($xml);
                $xpath = new DomXpath($dom);
                // find the data element with the matching attribute
                $node = $xpath->query("/config[@xmlns='http://www.linphone.org/xsds/lpconfig.xsd']/section[@name='auth_info_0']/entry[@name='username']");
                // assume there's only one, otherwise we can loop
                // clear the existing content
                $node[0]->textContent = ""; //<--- line 19
                

                // create a new string
                $fragment = $dom->createDocumentFragment();
                $fragment->appendXML($var);
                $node[0]->appendChild($fragment); //<--- line 25
                // save the updated XML
                file_put_contents("remote_prov2isioning.xml", $dom->saveXml());
            }
            
        ?>

I thought maybe the problem is about me, writing the path wrong but I check it couple of times and couldn't see anything wrong. I'm totally stuck. Thanks for any idea/comment..

CodePudding user response:

You need to account for namespaces in order to access the right element. Something like:

$xpath = new DOMXPath($dom);
$xpath->registerNamespace("xx", "http://www.linphone.org/xsds/lpconfig.xsd");

$target = $xpath->query("//xx:section[@name='auth_info_0']/xx:entry[@name='username']");
$target[0]->nodeValue = "john.doe";
echo $XMLDoc->saveXML($XMLDoc);

and you should see your expected output.

  • Related