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
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
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++; } }
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
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,