Note that there are some explanatory texts on larger screens.

plurals
  1. POHow can i append some elements to a particular element in xml using php?
    primarykey
    data
    text
    <p>I am trying to append some data in XML file using PHP.</p> <p>I am searching for a word if it is already present in XML or not.</p> <p>If it's present i only have to append the data to it otherwise i have to create whole element.</p> <p>For this i am using DOMXPath. Code is as follows:</p> <pre><code>$fname="ontology.xml"; $xml=new DomDocument; $xml-&gt;Load($fname); $xpath = new DOMXPath($xml); $query = '//RelatedTerms/words/word/word_title[.="'.$word.'"]'; $entries=$xpath-&gt;query($query); if($entries-&gt;length!=0) { foreach($entries as $entry) { $wordElement=$xml-&gt;getElementsByTagName($entry-&gt;parentNode-&gt;nodeName)-&gt;item(0); //Problem is here even if the &lt;word&gt; is found at the second position it append the element to the first &lt;word&gt; element. $newIsaElement=$xml-&gt;createElement('isa'); $newIsaTitleElement=$xml-&gt;createElement('isa_title',"sample"); $newRelevanceTitle=$xml-&gt;createElement('relevance',"9"); $newIsaElement-&gt;appendChild($newIsaTitleElement); $newIsaElement-&gt;appendChild($newRelevanceTitle); //$newIsaTitleElement-&gt;appendChild($newIsaElement); $wordElement-&gt;appendChild($newIsaElement); $xml-&gt;save($fname); } } else { echo "In Here!"; $words=$xml-&gt;getElementsByTagName('words')-&gt;item(0); $newWordElement=$xml-&gt;createElement('word'); $newWordTitleElement=$xml-&gt;createElement('word_title',$word); $newIsaElement=$xml-&gt;createElement('isa'); $newIsaTitleElement=$xml-&gt;createElement('isa_title',"test"); $newRelevanceTitle=$xml-&gt;createElement('relevance',"2"); $newWordElement-&gt;appendChild($newWordTitleElement); $newIsaElement-&gt;appendChild($newIsaTitleElement); $newIsaElement-&gt;appendChild($newRelevanceTitle); $newWordElement-&gt;appendChild($newIsaElement); $words-&gt;appendChild($newWordElement); $xml-&gt;save($fname); } </code></pre> <p>I want to add the element to the same element in which the word is found. Please help me through this!</p> <p>Thanks!</p> <p>PS:Here is a format of xml file if u need!</p> <pre><code>&lt;RelatedTerms&gt; &lt;words&gt; &lt;word&gt; &lt;word_title&gt;algo&lt;/word_title&gt; &lt;isa&gt; &lt;isa_title&gt;algorithm&lt;/isa_title&gt; &lt;relevance&gt;9&lt;/relevance&gt; &lt;/isa&gt; &lt;hasa&gt; &lt;hasa_title&gt;Algorithmic Example&lt;/hasa_title&gt; &lt;relevance&gt;7&lt;/relevance&gt; &lt;/hasa&gt; &lt;akindof&gt; &lt;akindof_title&gt;Pseudo Code&lt;/akindof_title&gt; &lt;relevance&gt;8&lt;/relevance&gt; &lt;/akindof&gt; &lt;partof&gt; &lt;partof_title&gt;Data Structures&lt;/partof_title&gt; &lt;relevance&gt;9&lt;/relevance&gt; &lt;/partof&gt; &lt;/word&gt; &lt;word&gt; &lt;word_title&gt;algo&lt;/word_title&gt; &lt;isa&gt; &lt;isa_title&gt;test&lt;/isa_title&gt; &lt;relevance&gt;9&lt;/relevance&gt; //instead of adding it here it adds to the above element &lt;/isa&gt; &lt;/word&gt; &lt;/words&gt; </code></pre> <p></p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload