Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6903673
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T07:57:18+00:00 2026-05-27T07:57:18+00:00

I have the following xml which i want to filter to show only data

  • 0

I have the following xml which i want to filter to show only data from a specify publisher.

    <?xml version="1.0"?>
<books>
  <book isbn="978-1594489501">
    <title>A Thousand Splendid Suns</title>
    <author>Khaled Hosseini</author>
    <publisher>Riverhead Hardcover</publisher>
    <amazon_price>14.27</amazon_price>
  </book>
  <book isbn="978-1594489587">
    <title>The Brief Wondrous Life of Oscar Wao</title>
    <author>Junot Diaz</author>
    <publisher>AB</publisher>
    <amazon_price>14.97</amazon_price>
  </book>
  <book isbn="978-0545010221">
    <title>Harry Potter and the Deathly Hallows</title>
    <author>J. K. Rowling</author>
    <publisher>AB</publisher>
    <amazon_price>19.24</amazon_price>
  </book>
</books>

i want to put in an html table only data from example publisher AB.

    <?php
// load SimpleXML
$books = new SimpleXMLElement("books.xml);
 $myVar="AB";  
 $books = $s->xpath("//book");
echo <<<EOF
<table style='width: 99%;'>
       <thead>
<tr>

                <th>Title</th>
                <th>Author</th>


       </tr>
</thead>

EOF;
foreach($books as $book) // loop through our books
{
           //if the type of $product books user input (ie $myVar)
       if ($book->type == $myVar) 
       {



        echo <<<EOF
        <tbody>
        <tr>

                <td>{$book->title} </td>
                <td>{$book->author}</td>


      </tr>


EOF;
 }}
 echo '</tbody>';
echo '</table>';
?>

I am getting Call to a member function xpath() on a non-object, any help please.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T07:57:18+00:00Added an answer on May 27, 2026 at 7:57 am

    If you want to load data from a file via the constructor of SimpleXMLElement you have to set the third parameter to true. Otherwise the string you pass as first parameter is interpreted as the “raw” xml data, i.e. either

    $x = new SimpleXMLELement('<books><book>....</books>');
    

    or

    $x = new SimpleXMLELement('books.xml', 0, true);
    

    or

    $x = simplexml_load_file('books.xml');
    

    and there are some other mistakes in the code. E.g. you want to test for a specific value in the <publisher> element but you are testing if ($book->type == $myVar).
    It’s probably better in this case to put the condition into the xpath expression

    <?php
    $doc = new SimpleXMLElement("books.xml", 0, true);
    $myVar="AB"; 
    $books = $doc->xpath("//book[publisher='$myVar']");
    ...
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have defined an XMLList which gets its data from the following XML file:
I have following xml and I want to fetch the value of node which
I have XML in the following format which I want to reformat: <blocks> <!--
i have the following xml (which i can not edit): <?xml version=1.0 encoding=UTF-8?> <ns0:prices
I have the following XML in a string named 'xml': <?xml version=1.0 encoding=ISO-8859-1?> <Book>
I have following xml which same child element (Filed) and I want to get
I want to read xml file which looks like the following ... I have
I have a big (1.9 GB) XML file which has data I want to
I have the following XML file with page nodes which I want to read
I have the following class which I want to serialize to XML: <Serializable()> _

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.