Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Search XML by node attribute in PHP

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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

Styling your websites can become very complex. Here I'll show how SASS can help you better organize, maintain and reuse your CSS code.
This article discusses how to implement server side field validation and display customized error messages to the client.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to dynamically set the form action using jQuery.
Suggested Courses

609 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