Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

XML Parse into array - how to access values?

Status
Not open for further replies.

Nematoth

Programmer
Mar 29, 2002
48
MY
Hi there,

I am new to parsing XML with PHP and have found a class that has parsed my XML from AFP very nicely into a multidimensional array:

Code:
Array
(
    [NewsML] => Array
        (
            [Catalog attr] => Array
                (
                    [Href] => [URL unfurl="true"]http://www.afp.com/dtd/AFPCatalog.xml[/URL]
                )

            [Catalog] => 
            [NewsEnvelope] => Array
                (
                    [DateAndTime] => 20051107T233033Z
                )

            [NewsItem] => Array
                (
                    [Identification] => Array
                        (
                            [NewsIdentifier] => Array
                                (
                                    [ProviderId] => afp.com
                                    [DateId] => 20051107
                                    [NewsItemId] => mmd--english--webwires--shared--football
                                    [RevisionId attr] => Array
                                        (
                                            [PreviousRevision] => 0
                                            [Update] => N
                                        )

                                    [RevisionId] => 1
                                    [PublicIdentifier] => urn:newsml:afp.com:20051107:mmd--english--webwires--shared--football:1
                                )

                            [NameLabel] => Football
                        )

                    [NewsManagement] => Array
                        (
                            [NewsItemType attr] => Array
                                (
                                    [FormalName] => News
                                )

                            [NewsItemType] => 
                            [FirstCreated] => 20051107T233033Z
                            [ThisRevisionCreated] => 20051107T233033Z
                            [Status attr] => Array
                                (
                                    [FormalName] => Usable
                                )

                            [Status] => 
                        )

                    [NewsComponent] => Array
                        (
                            [AdministrativeMetadata] => Array
                                (
                                    [Provider] => Array
                                        (
                                            [Party attr] => Array
                                                (
                                                    [FormalName] => AFP
                                                )

                                            [Party] => 
                                        )

                                )

                            [DescriptiveMetadata] => Array
                                (
                                    [Language attr] => Array
                                        (
                                            [FormalName] => en
                                        )

                                    [Language] => 
                                )

                            [NewsComponent] => Array
                                (
                                    [0] => Array
                                        (
                                            [NewsLines] => Array
                                                (
                                                    [HeadLine] => Nolan sends Bolton third
                                                )

                                            [NewsItemRef attr] => Array
                                                (
                                                    [NewsItem] => 051107231401.u0lwsyxa.xml
                                                )

                                            [NewsItemRef] => 
                                        )

                                    [1] => Array
                                        (
                                            [NewsLines] => Array
                                                (
                                                    [HeadLine] => Eto'o, Drogba among runners for CAF Player of the Year award
                                                )

                                            [NewsItemRef attr] => Array
                                                (
                                                    [NewsItem] => 051107153929.xld3y3yl.xml
                                                )

                                            [NewsItemRef] => 
                                        )

                                    [2] => Array
                                        (
                                            [NewsLines] => Array
                                                (
                                                    [HeadLine] => Old Firm prepare for cup clash
                                                )

                                            [NewsItemRef attr] => Array
                                                (
                                                    [NewsItem] => 051107211054.83jkvjaf.xml
                                                )

                                            [NewsItemRef] => 
                                        )

                                    [3] => Array
                                        (
                                            [NewsLines] => Array
                                                (
                                                    [HeadLine] => Chelsea won't suffer Gunners' fate, insists Mourinho
                                                )

                                            [NewsItemRef attr] => Array
                                                (
                                                    [NewsItem] => 051107180334.jakbj0oj.xml
                                                )

                                            [NewsItemRef] => 
                                        )

                                    [4] => Array
                                        (
                                            [NewsLines] => Array
                                                (
                                                    [HeadLine] => US book World Cup tune-up friendly with Norway
                                                )

                                            [NewsItemRef attr] => Array
                                                (
                                                    [NewsItem] => 051107161842.8vvlqprd.xml
                                                )

                                            [NewsItemRef] => 
                                        )

                                    [5] => Array
                                        (
                                            [NewsLines] => Array
                                                (
                                                    [HeadLine] => Blues hoping fit crew can steer ship out of trouble
                                                )

                                            [NewsItemRef attr] => Array
                                                (
                                                    [NewsItem] => 051107124651.2bwqr2j7.xml
                                                )

                                            [NewsItemRef] => 
                                        )

                                    [6] => Array
                                        (
                                            [NewsLines] => Array
                                                (
                                                    [HeadLine] => Shearer aims for end-of-the-month comeback after hernia op
                                                )

                                            [NewsItemRef attr] => Array
                                                (
                                                    [NewsItem] => 051107170901.hd0qouon.xml
                                                )

                                            [NewsItemRef] => 
                                        )

                                    [7] => Array
                                        (
                                            [NewsLines] => Array
                                                (
                                                    [HeadLine] => Hearts fans hostile to Rix appointment
                                                )

                                            [NewsItemRef attr] => Array
                                                (
                                                    [NewsItem] => 051107233029.v2hqnq5i.xml
                                                )

                                            [NewsItemRef] => 
                                        )

                                    [8] => Array
                                        (
                                            [NewsLines] => Array
                                                (
                                                    [HeadLine] => Football-hungry Desailly quits Qatar
                                                )

                                            [NewsItemRef attr] => Array
                                                (
                                                    [NewsItem] => 051107154300.ju7fncg5.xml
                                                )

                                            [NewsItemRef] => 
                                        )

                                    [9] => Array
                                        (
                                            [NewsLines] => Array
                                                (
                                                    [HeadLine] => United performance has silence critics: Ferdinand
                                                )

                                            [NewsItemRef attr] => Array
                                                (
                                                    [NewsItem] => 051107171104.nwi6annd.xml
                                                )

                                            [NewsItemRef] => 
                                        )

                                )

                        )

                )

        )

)

However... I cannot access the values at all..

For example.. I thought I could do this:

Code:
echo $data["NewsML"]["catalog"]["NewsItem"]["NewsComponent"]["NewsComponent"]["NewsLines"]["HeadLine"];

but this returns nothing..

How can I iterate through the [newComponent] array to display the headlines and the associated xml file with the content for that headline?

Any pointers greatly appreciated.. I have been banging my head for two days over this now.. :-(

Thanks.
N.

Nematoth
 
You forgot the array index that determines which story to show.
Code:
echo $data["NewsML"]["catalog"]["NewsItem"]["NewsComponent"]["NewsComponent"][1]["NewsLines"]["HeadLine"];
If you want to iterate through all the items, you should use a for or foreach loop depending on whether your iterating through assiciative or numbered array indices.
 
It is customary to explain the solution so that users after you could use this thread as a reference if suffering from the same problem. That way you give something back...
 
I was getting confused with all the multidimensional arrays so what I did was this:

Code:
	$newsArray = $data['NewsML']['NewsItem']['NewsComponent']['NewsComponent'];

	foreach($newsArray as $value)
	{
		foreach($value as $value2)
		{				
				if($value2['HeadLine']!="")
				{
					$headlineArr[] = $value2['HeadLine'];
				}
				if($value2['NewsItem']!="")
				{
					$xmlArr[] = $value2['NewsItem'];
				}				
		}
	}
[\code]

This gave me two simple arrays.. one with the headlines and one with the reference xml file attribute.. I was then able to display and link the a subsequent page that displayed the  xml data from the reference file using an xsl stylesheet.



Nematoth
[URL unfurl="true"]www.etv.com.my[/URL]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top