?
Solved

Search XML by node attribute in PHP

Posted on 2006-07-21
4
Medium Priority
?
371 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 1000 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 1000 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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

This article discusses how to create an extensible mechanism for linked drop downs.
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
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…
The viewer will learn how to dynamically set the form action using jQuery.
Suggested Courses

762 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