Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

xml: change node value using php

Posted on 2010-01-12
4
Medium Priority
?
1,291 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
  • 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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Preface This is the third article about the EE Collaborative Login Project. A Better Website Login System (http://www.experts-exchange.com/A_2902.html) introduces the Login System and shows how to implement a login page. The EE Collaborative Logi…
Without even knowing it, most of us are using web applications on a daily basis.  In fact, Gmail and Yahoo email, Twitter, Facebook, and eBay are used by most of us daily—and they are web applications. We generally confuse these web applications to…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
The viewer will receive an overview of the basics of CSS showing inline styles. In the head tags set up your style tags: (CODE) Reference the nav tag and set your properties.: (CODE) Set the reference for the UL element and styles for it to ensu…
Suggested Courses

571 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