0

Hopefully I'm asking this question correctly as I'm guessing this will need PHP to achieve.

What I would like to do is return a specific node and all children of XML data via a URL.

example: mydomain.com/myphpfile.php?book_id=1&title="My Book Title"

So by showing the URL above will return the following xml data only

   <Books>
     <book id="1">
        <title>My Book Title</title>
        <author>John Doe</author>
        <genre>Horror</genre>
       <description>Some long text description</description>
     </book>
  </Books>

This is just an example of XML:

 <Books>
     <book id="1">
        <title>My Book Title</title>
        <author>John Doe</author>
        <genre>Horror</genre>
       <description>Some long text description</description>
     </book>

       <book id="2">
        <title>My Book Title</title>
        <author>John Doe</author>
        <genre>Comedy</genre>
        <description>Some long text description</description>  
     </book>

       etc
 </Books>
4
  • use Xpath //book[id=$book_id and title=$title] Commented May 5, 2017 at 9:25
  • Thanks splash58 I'll give that a go - I have seen these sorts of replies in other posts but I wasn't sure - not my area of expertise. I'm guessing you add that in your xsl file? Commented May 5, 2017 at 9:32
  • It can be done by simplexml in php. Commented May 5, 2017 at 9:34
  • I'd be extremely grateful if you could give me an example or point me to somewhere with examples? Again not my area of knowledge :-) Commented May 5, 2017 at 9:41

1 Answer 1

1
 $book_id = 1;
 $title = 'My Book Title';

 $xml = simplexml_load_string($str);
 // find not suitable items - having incorrect id  or title
 $dels = $xml->xpath('//book[@id != '. $book_id .' or title != "'. $title .'"]');
  // delete found books from xml
  foreach($dels as $node) {
     unset($node->{0});
  }

echo  $xml->saveXML();
Sign up to request clarification or add additional context in comments.

5 Comments

Thank you! Can't wait to give it a try plus it gives me somewhere to start.Much appreciated
Just quickly how do you reference the XML file? i.e path to books.xml within your code?
use $xml = simplexml_load_file('books.xml ') - php.net/manual/en/function.simplexml-load-file.php
hey splash58 I changed the hard coded values for $book_id and $title to $book_id = $_GET['id'] and $title = $_GET[''title] now I can change the url parameters and XML data gets updated on refresh - I'm guessing that is correct?
Damn, I just ran into a problem what if the id contains letters i.e 287AA? This will be the case in my real project

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.