?
Solved

xml: change node value using php

Posted on 2010-01-12
4
Medium Priority
?
1,285 Views
Last Modified: 2013-12-12
Hello, i  need to change and/or delete some node from an xml config using php , i tried with SimpleXML but could not resolve the problem (newbie) . XML config example :
<?xml version="1.0" encoding="ISO-8859-1"?>
<config>
        <rule>
               <rule_id>1</rule_id>
                <enabled>yes</enabled>
                <access>allow</access>
                <name>Reminder</name>  // Ex. :change this by "NEW NAME2 " 
                <protocol>udp</protocol>
                <ports>1-1</ports>
                <ip>1.2.3.4</ip>
                <link>00</link>
        </rule>
// Delete rule 2
        <rule>
             <rule_id>2</rule_id>
                <enabled>yes</enabled>
                <access>allow</access>
                <name>Reminder2</name>
                <protocol>tcp</protocol>
                <ports>2-2</ports>
                <ip>6.2.5.4</ip>
                <link>01</link>
        </rule>
</config>
0
Comment
Question by:PabloRu
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
4 Comments
 

Author Comment

by:PabloRu
ID: 26299011
php 5.1.6
0
 
LVL 111

Accepted Solution

by:
Ray Paseur earned 2000 total points
ID: 26308770
That is a pretty back-level version of PHP and I do not have anything that old to test with, but please try this.  It is OK on PHP 5.2.6+

Best regards, ~Ray
<?php // RAY_temp_xml_example_137.php
error_reporting(E_ALL);
echo "<pre>\n"; // BETTER READABILITY

// THIS IS THE TEST DATA FROM THE POST AT EE
$xml = <<<EOXML
<?xml version="1.0" encoding="ISO-8859-1"?>
<config>
        <rule>
               <rule_id>1</rule_id>
                <enabled>yes</enabled>
                <access>allow</access>
                <name>Reminder</name>  // Ex. :change this by "NEW NAME2 "
                <protocol>udp</protocol>
                <ports>1-1</ports>
                <ip>1.2.3.4</ip>
                <link>00</link>
        </rule>
// Delete rule 2
        <rule>
             <rule_id>2</rule_id>
                <enabled>yes</enabled>
                <access>allow</access>
                <name>Reminder2</name>
                <protocol>tcp</protocol>
                <ports>2-2</ports>
                <ip>6.2.5.4</ip>
                <link>01</link>
        </rule>
</config>
EOXML;

// THIS IS THE NEW XML
$new = <<<EONEW
<?xml version="1.0" encoding="ISO-8859-1"?>
<config>
        <rule>
        </rule>
</config>
EONEW;

// LOAD THE XML INTO AN OBJECT
$obj = SimpleXML_Load_String($xml);

// ACTIVATE THIS TO SEE THE ORIGINAL OBJECT
// var_dump($obj);

// ITERATE OVER THE OBJECT TO CHANGE RULE 1
foreach ($obj->rule as $rule)
{
    if ($rule->rule_id == 1)
    {
        $rule->name = 'NEW NAME2';
    }
}

// ACTIVATE THIS TO SHOW THE UPDATED OBJECT
// var_dump($obj);


// ITERATE OVER THE OBJECT TO REMOVE RULE 2
$num = 0;
foreach ($obj->rule as $rule)
{
    $num++;
    if ($num == 2)
    {
        // NOTE THAT ARRAY NOTATION REQUIRES OFFSETS FROM ZERO [$num-1]
        unset($obj->rule[$num-1]);
        break;
    }
}

// SHOW THE UPDATED XML
echo htmlentities($obj->AsXML());

Open in new window

0
 

Author Closing Comment

by:PabloRu
ID: 31676423
Thank you.
0
 
LVL 111

Expert Comment

by:Ray Paseur
ID: 26356148
Thanks for the points - it's a great question!
0

Featured Post

Command Line Tips and Tricks

The command line is a powerful tool at the disposal of every Linux user. Although Linux distros come with beautiful user interfaces, it's worthwhile to learn the command line because it allows you to do a number of things that you otherwise cannot do from the GUI.  

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Many old projects have bad code, but the budget doesn't exist to rewrite the codebase. You can update this code to be safer by introducing contemporary input validation, sanitation, and safer database queries.
There are times when I have encountered the need to decompress a response from a PHP request. This is how it's done, but you must have control of the request and you can set the Accept-Encoding header.
HTML5 has deprecated a few of the older ways of showing media as well as offering up a new way to create games and animations. Audio, video, and canvas are just a few of the adjustments made between XHTML and HTML5. As we learned in our last micr…
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…
Suggested Courses

764 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question