Solved

Search XML by node attribute in PHP

Posted on 2006-07-21
4
358 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
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

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…
I found this questions asking how to do this in many different forums, so I will describe here how to implement a solution using PHP and AJAX. The logical flow for the problem should be: Write an event handler for the first drop down box to get …
The viewer will receive an overview of the basics of CSS showing inline styles. In the head tags set up your style tags: (CODE) Reference the nav tag and set your properties.: (CODE) Set the reference for the UL element and styles for it to ensu…
The viewer will the learn the benefit of plain text editors and code an HTML5 based template for use in further tutorials.

863 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

19 Experts available now in Live!

Get 1:1 Help Now