1

Hi there I have the following XML code:

<?xml version="1.0" encoding="UTF-8"?>
<connector_ret>
   <function name="search">
     <row id="1">
         <col id="1">AAA</col>
         <col id="2">243168</col>
         <col id="3">090828-000300</col>
         <col id="4">Subject</col>
     </row>
     <row id="2">
         <col id="1">BBB</col>
         <col id="2">243515</col>
         <col id="3">090831-000116</col>
         <col id="4">Subject</col>
     </row>
     <row id="3">
         <col id="1">BBB</col>
         <col id="2">244913</col>
         <col id="3">090905-000022</col>
         <col id="4">Subject</col>
     </row>
     <row id="4">
         <col id="1">CCC</col>
         <col id="2">245323</col>
         <col id="3">090907-000253</col>
         <col id="4">Subject</col>
     </row>
     <row id="5">
         <col id="1">CCC</col>
         <col id="2">245323</col>
         <col id="3">090907-000253</col>
         <col id="4">Subject</col>
     </row>
   </function>
</connector_ret>

I was wondering if it was possible to loop through this in PHP4 and only display certain rows when given a variable.

For example if the variable given is BBB then only display rows where the first column id is also equal to BBB.

So the output would be:

BBB - 243515 - 090831-000116 - Subject BBB - 244913 - 090905-000022 - Subject

and the other rows are just ignored.

If the variable given is AAA the output would be:

AAA - 243168 - 090828-000300 - Subject

You get the idea :)

If you can help that'd be fantastic. Thanks so much.

3
  • 1
    @Pete: just a remark PHP4 is dead so you should think about upgrading Commented Nov 19, 2009 at 7:41
  • Trust me it'd make things a lot easier but trying to get IT at work to switch to 5 is nearly impossible. Commented Nov 19, 2009 at 7:42
  • @Pete: I am pretty much in the same situation here, I have installed both PHP5/PHP4 on the webserver so at least every new stuff is in PHP5. The problem if you don't start you cannot migrate ;-) Commented Nov 19, 2009 at 7:58

2 Answers 2

2

you should use xpath to do such processing

$dom    =domxml_open_mem($xml); 
$calcX = &$dom->xpath_new_context();
$xml_parsed["match"]= node_content(
   $calcX->xpath_eval("//row/col[@id=1][text()='BBB']")
);
Sign up to request clarification or add additional context in comments.

5 Comments

Are there any prerequisites to this? I have a feeling IT have disabled the modules needed for this as PHP conks out as it runs the domxml_open_mem function.
yeah the domxml is a PECL extension, but on most modern distro (Ubuntu,debian,centos) you can install has a package, see php.net/manual/en/domxml.installation.php for PHP install
Thanks for that. Is there any solution for this out of the box? I'm afraid trying to get IT to add any addition extensions might have issues. If not I guess that's the only way I can go about it. Thanks again!
that's not the only way but after doing some reg exp parsing of XML/HTML is evil, so you better to get your sysroot moving.
Thanks Rage. I'll definitely try the push to move forward. Thanks for all your help!
0

You might look at XSL-T as your solution to this. It's easy enough to select the nodes you want and transform the results.

2 Comments

Problem is the XML is generated through a third-party API and as such I'm only able to work with what the XML output I have.
@Bernard: XSL-T is available out of the box only in PHP5 and for doing such things plain Xpath is enough IMHO

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.