Solved

Traversing through XML to retrieve data via SimpleXML

Posted on 2010-09-24
8
979 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
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 
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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

Suggested Solutions

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…
Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
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 create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

839 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