Solved

Search XML by node attribute in PHP

Posted on 2006-07-21
4
366 Views
Last Modified: 2013-11-19
Hi,

 I have the xml file given below. Here I want to get the values based on the id(attribute) of node page. Means I want that that particular node's all attribute values that matches with the attribute 'id'.



<map>
<page id="home" name="Home" content="home.xml" template="home.php" bar="home_bar.xml"/>

<page id="training" name="Training" content="training.xml" template="text.php" bar="training_bar.xml">
         <page id="training-trainings" name="Trainings" content="training-trainings.xml" template="text.php" bar="training_bar.xml"/>
        <page id="training-workshops" name="Workshops" content="training-workshops.xml" template="text.php" bar="training_bar.xml"/>
        <page id="training-agenda" name="Agenda" content="training-agenda.xml" template="text.php" bar="training_bar.xml"/>
</page>

<page id="products" name="Products" content="products.xml" template="text.php" bar="products_bar.xml">
        <page id="products-brands" name="Brands" content="products-brands.xml" template="text.php" bar="products_bar.xml"/>
       <page id="products-offers" name="Offers" content="products-offers.xml" template="text.php" bar="products_bar.xml"/>
</page>
</map>

Thanks,
Looking forward for your help
0
Comment
Question by:gstalin
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
4 Comments
 
LVL 22

Accepted Solution

by:
Ivo Stoykov earned 250 total points
ID: 17152206
Hello gstalin,

There is a XMLReader extension is an XML Pull parser. The reader acts as a cursor going forward on the document stream and stopping at each node on the way.

XMLReader->getAttribute() - Get value of attribute by name

HTH

I
0
 
LVL 9

Assisted Solution

by:cristi_2112
cristi_2112 earned 250 total points
ID: 17152938
If you don't want to parse the entire file usign PHP's extensions (there are plenty examples on http://www.php.net/manual/en/ref.xml.php), you could do something like:
<?php
$id = 1; // page_id you're looking for
$file = "yourfile.xml"; // xml file

$search_str = '<page id="' . $id . '"';
if (!($fp = fopen($file, "r"))) {
   die("could not open XML input");
}
while ($getline = fread($fp, 4096)) {
   if (strpos($getline, '$search_str)) {
      $found = 1;
      break;
   }
}
// now $getline contains the attributes of the searched tag
// you can var_dump($getline);
// and then explode ($getline, ' ');
// and whatever else you need
?>
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
What does GoogleTagMgr javascripts below do 5 55
Checking https returns 301 21 56
Php - How to get the value of json file using PHP? 29 58
xml with php question 5 33
What is Node.js? Node.js is a server side scripting language much like PHP or ASP but is used to implement the complete package of HTTP webserver and application framework. The difference is that Node.js’s execution engine is asynchronous and event…
Introduction Knockoutjs (Knockout) is a JavaScript framework (Model View ViewModel or MVVM framework).   The main ideology behind Knockout is to control from JavaScript how a page looks whilst creating an engaging user experience in the least …
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

756 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question