Solved

Traversing through XML to retrieve data via SimpleXML

Posted on 2010-09-24
8
968 Views
Last Modified: 2012-05-10
Hi, please forgive my stupid question. I am not very familiar with PHP but need it for a relatively simple task.

I have an XML document in the format below:

<autnresponse xmlns:autn="http://schemas.autonomy.com/aci/">
      <responsedata>
            <autn:hit>
                  <autn:content>
                        <RECORD>
                              <FORM>
                                    <Q>unique identifier</Q>
                                    <A>some value</A>
                                    <VALUE>some other value</VALUE>
                              </FORM>
                              <FORM>
                                    <Q>unique identifier</Q>
                                    <A>some value</A>
                                    <VALUE>some other value</VALUE>
                              </FORM>
                        </RECORD>
                  </autn:content>
            </autn:hit>
      </responsedata>
</autnresponse>

Basically all I need is for simplexml_load_file() to read through that and retrieve all the <A> and <VALUE> fields, and put them in an array to use later. I'm having a hard time getting it right and PHP error messages aren't very helpful as to what I'm doing wrong.. especially when I don't get any error messages. For instance:

<?php

$urlrequest = "the url";
$xml = simplexml_load_file($urlrequest);
$num = "0";
foreach ($xml->responsedata->hit as $value){
    $num++;
}
echo $num;
?>

With the above XML, shouldn't num be 2? It always comes out 0...
0
Comment
Question by:Tabris42
  • 6
  • 2
8 Comments
 
LVL 30

Expert Comment

by:Marco Gasi
Comment Utility
You fail to get correct values because twqo element's names contain colon (:): it's to avoid to use colon in element's names because it's reserved for namespace. So you should modify tha way you name xml elements if you name them dinamically or simple rename them. Once you have renamed elements you can use this code:

$num = "0";
foreach ($xml->responsedata->autnhit->autncontent->RECORD->FORM as $value){
    echo "scusa ".$value."<br>";
    $num++;
}
echo $num;

Keep in mind php is case-sensitive so you have to use lower-case and uppercase accordingly to your xml file element's names.

Hope this help

Cheers
0
 

Author Comment

by:Tabris42
Comment Utility
Unfortunately, I have no control over the actual XML document, so I'm not able to remove the colons. Is it possible to escape them out somehow?
0
 
LVL 30

Expert Comment

by:Marco Gasi
Comment Utility
As far as I know, you can't escape element names for the simple reason that, to escaping them you have to get them and you can't them because they contain colon (or other reserved char like . , - and so on).

I'm trying to test some namespaces function: I'll let you know if I'll get something...

Bye
0
 
LVL 30

Expert Comment

by:Marco Gasi
Comment Utility
Try this:

$num = "0";
foreach ($xml->xPath("//FORM") as $value){
    $num++;
}
echo $num;

This way it return correct value. Anyway, I'll look for a better method toùùusing xPath with namespaces...

Cheers
0
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 30

Expert Comment

by:Marco Gasi
Comment Utility
I tried some code using namespaces associated to xpath but I didn't get anyy apreciable result. The only method I've found to count xml elements is what I've posted before: can it help you?

Cheers
0
 

Author Comment

by:Tabris42
Comment Utility
Sorry for the slow responses, I was away from my code during the weekend.

We seem to be making headway with this XPATH. If I do this:
$res = $xml->xpath("responsedata/autn:hit/autn:content");

The 5 records are returned. However, I am oblivious to how to access. If I do this:
$print_r($res);
The output is this:
[RECORD] => SimpleXMLElement Object ( [FORM] => Array ( [0] => SimpleXMLElement Object ( [Q] => unique identifier [A] => some value [VALUE] => some other value ) [1] => SimpleXMLElement Object ( [Q] => unique identifier [A] => some value [VALUE] => some other value)))

Which is certainly what I like to see. My questions is, how do I access those "Q" and "A" and "VALUE"  fields I am seeing from $print_r()? Sorry if I am asking very basic PHP questions here, but I am learning as I go and not really well trained with it.
0
 
LVL 30

Accepted Solution

by:
Marco Gasi earned 500 total points
Comment Utility
Try if this can work for you:

$res = $xml->xpath("responsedata/autn:hit/autn:content/RECORD/FORM/Q");
foreach ($res as $r){
    echo $r."<br>";
}

Obviously you have to adjust for other elements:
$res = $xml->xpath("responsedata/autn:hit/autn:content/RECORD/FORM/A");
$res = $xml->xpath("responsedata/autn:hit/autn:content/RECORD/FORM/VALUE");

Cheers
0
 
LVL 30

Expert Comment

by:Marco Gasi
Comment Utility
Glad my solution helped you. Thanks for points

Bye
0

Featured Post

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Deprecated and Headed for the Dustbin By now, you have probably heard that some PHP features, while convenient, can also cause PHP security problems.  This article discusses one of those, called register_globals.  It is a thing you do not want.  …
I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

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

13 Experts available now in Live!

Get 1:1 Help Now