Solved

Traversing through XML to retrieve data via SimpleXML

Posted on 2010-09-24
8
978 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!

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.

 
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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

Suggested Solutions

Introduction In my previous article (http://www.experts-exchange.com/Microsoft/Development/MS-SQL-Server/SSIS/A_9150-Loading-XML-Using-SSIS.html) I showed you how the XML Source component can be used to load XML files into a SQL Server database, us…
Many times as a report developer I've been asked to display normalized data such as three rows with values Jack, Joe, and Bob as a single comma-separated string such as 'Jack, Joe, Bob', and vice versa.  Here's how to do it. 
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…
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…

776 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