Solved

Search XML by node attribute in PHP

Posted on 2006-07-21
4
357 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
4 Comments
 
LVL 22

Accepted Solution

by:
Ivo Stoykov earned 250 total points
Comment Utility
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
Comment Utility
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

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

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 …
Styling your websites can become very complex. Here I'll show how SASS can help you better organize, maintain and reuse your CSS code.
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…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

744 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now