Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>From what i understand you want to pull out a xml element and save it as another xml file</p> <p>if so use this</p> <pre class="lang-php prettyprint-override"><code>$xmlassignDoc = new DOMDocument(); $xmlassignDoc-&gt;load("book.xml"); $books = $xmlassignDoc-&gt;getElementsByTagName("item"); $singleBook = new DOMDocument('1.0', 'utf-8'); $singleBook-&gt;formatOutput = true; foreach($books as $book) { $bookID = $book-&gt;getAttribute("id"); //HERE is where the GET function will be if ($bookID == '483') { $singleBook-&gt;loadXML("&lt;book&gt;&lt;/book&gt;"); $node = $singleBook-&gt;importNode($book, true); $singleBook-&gt;documentElement-&gt;appendChild($node); echo $singleBook-&gt;saveXML(); //do we need to save?!?! } } </code></pre> <p>result:</p> <pre class="lang-xml prettyprint-override"><code>&lt;?xml version="1.0"?&gt; &lt;book&gt; &lt;item id="483"&gt; &lt;title&gt;Database systems management and design 1&lt;/title&gt; &lt;isbn&gt;0877091153&lt;/isbn&gt; &lt;url&gt;http://library.hud.ac.uk/catlink/bib/483&lt;/url&gt; &lt;borrowedcount&gt;28&lt;/borrowedcount&gt; &lt;courses&gt; &lt;course&gt;CC140&lt;/course&gt; &lt;/courses&gt; &lt;/item&gt; &lt;/book&gt; </code></pre> <p>or if you only want the id as a result</p> <pre class="lang-php prettyprint-override"><code>if ($bookID == '483') { $singleBook-&gt;loadXML("&lt;book&gt;&lt;/book&gt;"); $node = $singleBook-&gt;createElement("id", $bookID); //&lt;--- ID only $singleBook-&gt;documentElement-&gt;appendChild($node); echo $singleBook-&gt;saveXML(); } </code></pre> <p>result</p> <pre class="lang-xml prettyprint-override"><code>&lt;?xml version="1.0"?&gt; &lt;book&gt; &lt;id&gt;483&lt;/id&gt; &lt;/book&gt; </code></pre> <p>In your case you want the output to be </p> <pre class="lang-xml prettyprint-override"><code>&lt;results&gt; &lt;book id="1234" title="Interaction Design" isbn="968347337" borrowedcount="15"/&gt; &lt;/results&gt; </code></pre> <p>use this</p> <pre class="lang-php prettyprint-override"><code>$xmlassignDoc = new DOMDocument(); $xmlassignDoc-&gt;load("books.xml"); $books = $xmlassignDoc-&gt;getElementsByTagName("item"); $singleBook = new DOMDocument('1.0', 'utf-8'); $singleBook-&gt;formatOutput = true; foreach($books as $book) { $bookID = $book-&gt;getAttribute("id"); //HERE is where the GET function will be if ($bookID == '483') { $singleBook-&gt;loadXML("&lt;results&gt;&lt;/results&gt;"); $element = $singleBook-&gt;createElement("book"); //id attribute $attrId = $singleBook-&gt;createAttribute('id'); $attrId-&gt;value = $bookID; //title attribute $title = $book-&gt;getElementsByTagName("title"); $title = $title-&gt;item(0)-&gt;nodeValue; $attrTitle = $singleBook-&gt;createAttribute('title'); $attrTitle-&gt;value = $title; //the rest of the attributes //add the attributes to the element $element-&gt;appendChild($attrId); $element-&gt;appendChild($attrTitle); //add the element to the dox $singleBook-&gt;documentElement-&gt;appendChild($element); //output the result (don't forget to save if needed :) ) echo $singleBook-&gt;saveXML(); } } </code></pre>
    singulars
    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.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      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