Solved

Traversing through XML to retrieve data via SimpleXML

Posted on 2010-09-24
8
984 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
[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
  • 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

[Webinar] Learn How Hackers Steal Your Credentials

Do You Know How Hackers Steal Your Credentials? Join us and Skyport Systems to learn how hackers steal your credentials and why Active Directory must be secure to stop them. Thursday, July 13, 2017 10:00 A.M. PDT

Question has a verified solution.

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

Browsing the questions asked to the Experts of this forum, you will be amazed to see how many times people are headaching about monster regular expressions (regex) to select that specific part of some HTML or XML file they want to extract. The examp…
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…
The viewer will learn how to dynamically set the form action using jQuery.

688 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