Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8509959
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T03:34:33+00:00 2026-06-11T03:34:33+00:00

I know this question is asked many times before but still can’t find the

  • 0

I know this question is asked many times before but still can’t find the desired result I have been looking for parsing xml document using php DOMDocument function.

Initially my xml document named ‘local.xml’ contains the following script

<?xml version="1.0" encoding="utf-8"?>
<config>
    <global>
        <host><![CDATA[name123]]></host>
        <username><![CDATA[root45]]></username> 
    </global>
</config>

I want to change “<host><![CDATA[name123]]></host>” to “<host><![CDATA[name456]]></host>“

I have php code that can change the value inside tag. Here is the php code

<?php
$doc = new DOMDocument();
$doc->load('local.xml');
$config = $doc->getElementsByTagName( "global" );
foreach( $config as $global )
{

    $hosts=$global->getElementsByTagName( "host" );
    $usernames=$global->getElementsByTagName( "username" );
    $a = "<![CDATA[name456]]>";
    $hosts1=$hosts->item(0)->nodeValue=$a;
}
$doc->save('local.xml');
?>

When i execute the above code, the output in the xml file is coming as

<?xml version="1.0" encoding="utf-8"?>
<config>
    <global>
        <host>&lt;![CDATA[name456]]&gt;</host>
        <username><![CDATA[root45]]></username> 
    </global>
</config>

How can I convert "&lt;" to "<" and "&gt;" to ">" in xml document?

I have tried $a = htmlspecialchars("<![CDATA[name456]]>"); as well as $a = html_entity_decode("<![CDATA[name456]]>"); still it is giving the same output as seen above.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T03:34:35+00:00Added an answer on June 11, 2026 at 3:34 am

    Finally After some more hours and hours of trails and errors, I managed to solve it. Had Daniel Sloof not shown the tip, my code would have been useless. Thanks Daniel Sloof

    here is the final code

    <?php
    $doc = new DOMDocument();
    $doc->load('local.xml');
    $config = $doc->getElementsByTagName( "global" );
    foreach( $config as $global )
    {
        $hosts=$global->getElementsByTagName( "host" );
        $oldcdata=$hosts->item(0)->nodeValue="";
        $newcdata=$doc->createCDATASection("name456");
        $hosts1=$hosts->item(0)->appendChild($newcdata);
    }
    $doc->save('local.xml');
    ?>
    

    By adding null value to $oldcdata it will empty the value inside <host><![CDATA[name123]]></host> tag this will become <host></host> in xml file

    Now we add a new value to $newcdata it will then be appended to <host></host> by using appendChild($newcdata) and it will become <host><![CDATA[name456]]></host>

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I know this question has been asked many times before but I can't find
I know this question was asked here many times before but I am still
I know i have asked this question many times but i still havnt found
I know this question have been asked endless times but I can't find a
I know this is a question that has been asked many times before, but
I know this question was asked many times, but still my problem is not
i know you will say that this question is asked before many times but
This question seems to have been asked many times before, but I didn't see
I know this question has been asked so many times before but I couldn't
I know this question has been asked many times before, but any of the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.