is it possible to add element after particular element in a xml file

Discussion in 'XML & RSS' started by shiva_rnm, Mar 17, 2009.

  1. #1
    Hi All,

    say suppose xml file,
    <a>
    <c3>text</c3>
    <c1>stuff</c1>
    </a>

    I want to add <c2> new </c2> like

    <a>
    <c3>text</c3>
    <c2> new </c2>
    <c1>stuff</c1>
    </a>

    ...

    I can add an element at the end
    <a>
    <c3>text</c3>
    <c1>stuff</c1>
    <c2> new </c2>
    </a>

    I am making use of the SimpleXML library in a PHP 5.

    any help wil be appreciated .

    Thanks
     
    shiva_rnm, Mar 17, 2009 IP
  2. Stylesofts

    Stylesofts Peon

    Messages:
    64
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Hello,

    Yes,definately you can add it in that way..you can add anything anyway you can.. Are you creating the XML manaually or through query?


    Regards,
    Stylesofts Developing Team
     
    Stylesofts, Mar 17, 2009 IP
  3. shiva_rnm

    shiva_rnm Peon

    Messages:
    20
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Thanks for reply...am creating xml through query

    it would be helpful if u give me code...

    I am giving code which adds node at the last level...

    $xml = simplexml_load_string($xml);
    $r = $xml->xpath('//b');
    foreach ($r[0] as $a)
    {
    if($a[0]->getName() == 'c')
    {
    $ad = "c2";
    $r[0]->$ad = "new";
    $fp = @fopen('test.xml', "w");
    @fwrite($fp, $xml->asXML());
    @fclose($fp);
    break;
    $i++;
    }
    }
     
    shiva_rnm, Mar 17, 2009 IP
  4. Stylesofts

    Stylesofts Peon

    Messages:
    64
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Hello,

    If you are using an query to make the xml then why do u need to add node at the last. Instead of that u can simply make a new xml...Why are u making it so complicated.
    Well if we were you we would like this and make new instead of adding nodes .

    
    $xml="<?xml version='1.0' encoding='utf-8' standalone='yes'?>";
    $fileContent = $xml."<articles>";
    
    ////////Exporting TO XML///////////////
    $selectQuery = "SELECT * FROM tbl_articles ORDER BY id DESC";
    $objNews->query($selectQuery);
    while($rowSponsers=$objNews->query_fetch())
    {
    
    	$xmlContent="<NewsFeed>
    	<c3>$rowSponsers[articleName]</c3>
    	<c1>$rowSponsers[createdDate]</c1>
    	<c2>$rowSponsers[writersName]</c2>
    
    	</NewsFeed>";
    	$xmlContent1 = $xmlContent1.$xmlContent;
    }
    $fileContent = $fileContent.$xmlContent1;
    $fileEnd="</articles>";					
    $fileContent= $fileContent.$fileEnd;	
    			
    $file = "news.xml";
    
    $handle = fopen($file,"w");
    fwrite($handle,$fileContent);
    
    
    
    PHP:
    Regards,

    Stylesofts Developing Team
     
    Stylesofts, Mar 18, 2009 IP
  5. shiva_rnm

    shiva_rnm Peon

    Messages:
    20
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Hi, Sorry am not using query for creating the xml. I am creating it manaually. I had given above code also.
    could you please help me in this adding node in a particular place.

    Thanks,
     
    shiva_rnm, Mar 20, 2009 IP
  6. Stylesofts

    Stylesofts Peon

    Messages:
    64
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #6
    hello

    plz refer to our previous message..


    Regards
    Stylesofts Developing Team
     
    Stylesofts, Mar 20, 2009 IP