tags:

views:

1275

answers:

4

How can I delete and edit a specific node within a XML file by using php? please healp me anybody!!

+1  A: 

Take a look at this SO question. Also, this tutorial describes a few different methods to work with an XML document in php.

andi
A: 

You could use the xml2array function, and then threat it like an array:

function xml2array($contents, $get_attributes=1){
if(!$contents) return array();
if(!function_exists('xml_parser_create')){
 print "'xml_parser_create()' function not found!";
 return array();
}
//Get the XML parser of PHP - PHP must have this module for the parser to work
$parser = xml_parser_create();
xml_parser_set_option( $parser, XML_OPTION_CASE_FOLDING, 0 );
xml_parser_set_option( $parser, XML_OPTION_SKIP_WHITE, 1 );
xml_parse_into_struct( $parser, $contents, $xml_values );
xml_parser_free( $parser );
if(!$xml_values) return;//Hmm...
//Initializations
$xml_array = array();
$parents = array();
$opened_tags = array();
$arr = array();
$current = &$xml_array;
//Go through the tags.
foreach($xml_values as $data){
 unset($attributes,$value);//Remove existing values, or there will be trouble
 //This command will extract these variables into the foreach scope
 // tag(string), type(string), level(int), attributes(array).
 extract($data);//We could use the array by itself, but this cooler.
 $result = '';
 if($get_attributes){//The second argument of the function decides this.
  $result = array();
  if(isset($value)) $result['value'] = $value;
  //Set the attributes too.
  if(isset($attributes)){
   foreach($attributes AS $attr => $val){
    if($get_attributes == 1) $result['attr'][$attr] = $val; //Set all the attributes in a array called 'attr'
    /**  :TODO: should we change the key name to '_attr'? Someone may use the tagname 'attr'. Same goes for 'value' too */
   }
  }
 }else if(isset($value)){
  $result = $value;
 }
 //See tag status and do the needed.
 if($type == "open"){//The starting of the tag '<tag>'
  $parent[$level-1] = &$current;
  if(!is_array($current) OR (!in_array($tag, array_keys($current)))){//Insert New tag
   $current[$tag] = $result;
   $current = &$current[$tag];
  }else{ //There was another element with the same tag name
   if(isset($current[$tag][0])){
    array_push($current[$tag], $result);
   }else{
    $current[$tag] = array($current[$tag],$result);
   }
   $last = count($current[$tag]) - 1;
   $current = &$current[$tag][$last];
  }
 }else if($type == "complete"){//Tags that ends in 1 line '<tag />'
  //See if the key is already taken.
  if(!isset($current[$tag])){//New Key
   $current[$tag] = $result;
  }else{//If taken, put all things inside a list(array)
   if((is_array($current[$tag]) and $get_attributes == 0)//If it is already an array...
     or (isset($current[$tag][0]) and is_array($current[$tag][0]) and $get_attributes == 1)){
    array_push($current[$tag],$result); // ...push the new element into that array.
   }else{//If it is not an array...
    $current[$tag] = array($current[$tag],$result); //...Make it an array using using the existing value and the new value
   }
  }
 }else if($type == 'close'){ //End of tag '</tag>'
  $current = &$parent[$level-1];
 }
}
return($xml_array);
}
DaNieL
thanks for your codings. i used it.
i used it to parse xml to array and editted the array. how will i change the array to xml?
You need the function inverse, array2xml.You can try this to:http://phpclasses.burina.net/browse/package/2286.htmlhttp://snipplr.com/view/3491/convert-php-array-to-xml-or-simple-xml-object-if-you-wish/or else, write down your own function to fulfill your needs...Oh, give at look to this one to:
DaNieL
+1  A: 

I use the DOM library to traverse and manipulate XML documents.

Before you can delete or edit a node, you will need to load your XML document into a DOMDocument object:

$domDocument = new DOMDocument();
$domDocument->load('youfile.xml');

There are many ways to find the node you are after. One of them is performing a query using the XPath for the node:

$xmlPath = new DOMXPath($domDocument);
$xmlNodes = $xmlPath->query('//graphics');

$xmlNodes is an array of DOMElements. They have their own functions to delete or edit the particular nodes you are after.

For example you can get all of them and delete certain attributes:

foreach($xmlNodes as $node)
{
    $node->removeAttribute('size');
}

Once you are done, you can save your changes to file:

$domDocument->save('yourfile.xml');
Marcel Tjandraatmadja
is there graphics is the name of the node? can you please tell me with the following xml file?<new><message id='1'> <first>Abisha</first> <last>MG</last> <company>Zen</company> <email>[email protected]</email> <gender>Female</gender></message></new>i want edit Abisha means?
A: 

SimpleXML was new to PHP 5.

<?php
    $xml = simplexml_load_file("test.xml");
?>

Once your XML is loaded into a simpleXML object you can locate the specific node and 'unset' it to delete it.

Jon Winstanley