Solved

Traversing through XML to retrieve data via SimpleXML

Posted on 2010-09-24
8
981 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 31

Expert Comment

by:Marco Gasi
ID: 33754710
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
ID: 33754859
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 31

Expert Comment

by:Marco Gasi
ID: 33755138
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
Guide to Performance: Optimization & Monitoring

Nowadays, monitoring is a mixture of tools, systems, and codes—making it a very complex process. And with this complexity, comes variables for failure. Get DZone’s new Guide to Performance to learn how to proactively find these variables and solve them before a disruption occurs.

 
LVL 31

Expert Comment

by:Marco Gasi
ID: 33755261
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
 
LVL 31

Expert Comment

by:Marco Gasi
ID: 33760718
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
ID: 33769133
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 31

Accepted Solution

by:
Marco Gasi earned 500 total points
ID: 33769332
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 31

Expert Comment

by:Marco Gasi
ID: 33769849
Glad my solution helped you. Thanks for points

Bye
0

Featured Post

How Do You Stack Up Against Your Peers?

With today’s modern enterprise so dependent on digital infrastructures, the impact of major incidents has increased dramatically. Grab the report now to gain insight into how your organization ranks against your peers and learn best-in-class strategies to resolve incidents.

Question has a verified solution.

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

This article discusses four methods for overlaying images in a container on a web page
This article discusses how to create an extensible mechanism for linked drop downs.
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…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

679 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