Home > Blockchain >  Xml Append/Add new node php
Xml Append/Add new node php

Time:05-27

I'm trying to update and add node to a xml file. Currently I can create and overwrite the file with my new node, however what I need to do is do to is add the new node to the existing file (.xml) and I am exhausted. I am new to php (I've tried all every code on this site and this is my current code can't be added here ... please Help

$doc = new DOMDocument; 
// Load the XML 
///$doc->loadXML("<root/>"); 
//---- ///$xml = new Document; 
///$xml ->loadXML($xml); 
//$xml = simplexml_load_file("pole.xml"); 
$title = $_POST["title"]; 
$xml = <<<XML <item> <title>$title</title> </item> XML; 
$xml = new Document; 
$xml ->loadXML($xml); 
$xml ->appendXML($xml); 
$xml = new SimpleXMLElement($xml); 
echo $xml->saveXML('pole.xml'); 

CodePudding user response:

I can offer no advice for using SimpleXML but as the above does attempt at using DOMDocument perhaps the following simple example will be of use.

    $filename='pole.xml';

    # Stage 1
    # -------
    
    // Create an instance of DOMDocument and then
    // generate whatever XML you need using DOMDocument
    // and save.
    libxml_use_internal_errors( true );

    $dom=new DOMDocument('1.0','utf-8');
    $dom->formatOutput=true;
    $dom->preserveWhiteSpace=true;
    
    $root=$dom->createElement('Root');
    $dom->appendChild( $root );

    $item=$dom->createElement('Item');
    $title=$dom->createElement('title','Hello World');
    
    $item->appendChild( $title );
    $root->appendChild( $item );
    
    $dom->save( $filename );
    $dom=null;

This yields the following XML:

<?xml version="1.0" encoding="utf-8"?>
<Root>
  <Item>
    <title>Hello World</title>
  </Item>
</Root>

To then modify the XML file you have created or downloaded etc:

    # Stage 2
    # -------
    
    // A new instance of DOMDocument is NOT strictly necessary here
    // if you are continuing to work with the generated XML but for the purposes
    // of this example assume stage 1 and stage 2 are done in isolation.
    
    // Find the ROOT node of the document and then add some more data...
    // This simply adds two new simple nodes that have various attributes
    // but could be considerably more complex in structure.
    
    
    $dom=new DOMDocument;
    $dom->formatOutput=true;
    $dom->preserveWhiteSpace=false;
    $dom->load( $filename );

    # Find the Root node... !!important!!
    $root=$dom->getElementsByTagName('Root')->item(0);

    # add a new node
    $item=$dom->createElement('Banana','Adored by monkeys');
    $attributes=array(
        'Origin'    =>  'Central America',
        'Type'      =>  'Berry',
        'Genus'     =>  'Musa'
    );
    foreach( $attributes as $attr => $value ){
        $attr=$dom->createAttribute( $attr );
        $attr->value=$value;
        $item->appendChild( $attr );
    }
    #ensure that you add the new node to the dom
    $root->appendChild( $item );
    
    
    #new node
    $item=$dom->createElement('Monkey','Enemies of Bananas');
    $attributes=array(
        'Phylum'    =>  'Chordata',
        'Class'     =>  'Mammalia',
        'Order'     =>  'Primates'
    );
    foreach( $attributes as $attr => $value ){
        $attr=$dom->createAttribute( $attr );
        $attr->value=$value;
        $item->appendChild( $attr );
    }
    $root->appendChild( $item );
    
    
    $dom->save( $filename );
    $dom=null;

This modifies the XML file and yields the following:

<?xml version="1.0" encoding="utf-8"?>
<Root>
  <Item>
    <title>Hello World</title>
  </Item>
  <Banana Origin="Central America" Type="Berry" Genus="Musa">Adored by monkeys</Banana>
  <Monkey Phylum="Chordata" Class="Mammalia" Order="Primates">Enemies of Bananas</Monkey>
</Root>
  • Related