XML Parsing getting Xpath

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
maquejp
Forum Newbie
Posts: 2
Joined: Mon Jan 07, 2008 8:57 am

XML Parsing getting Xpath

Post by maquejp »

feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


Hello,

I am trying to retrieve the xpath of an xml file.
The xml file looks
[syntax="xml"]<CONF_2008 xsi:noNamespaceSchemaLocation="Conference.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
	<OrgLegalName>a</OrgLegalName>
	<OrgAcronym>a</OrgAcronym>
....
	<HumanResources>
		<HRFunction>a</HRFunction>
		<HRNberOfStaff>1</HRNberOfStaff>
		<HRCost>1</HRCost>
		<HRManMonth>1</HRManMonth>
		<HRComments>a</HRComments>
	</HumanResources>
	<E1Staff>1234567891</E1Staff>
	<I6OtherCurrentFunding>0</I6OtherCurrentFunding>
</CONF_2008>
I try to display a list of the xpath (to stored then in a after).


The result I try to have should be like:

/conf_2008/OrgLegalName
/conf_2008/OrgAcronym
...
/conf_2008/HumanResources
/conf_2008/HumanResources/HRFunction
/conf_2008/HumanResources/HRNberOfStaff
....
/conf_2008/I6OtherCurrentFunding


I am for the moment with something like this:[/syntax]

Code: Select all

<?php
function parseXML($node,$parent)
{
    if (!$node->read())
        return;
    //If this is a text node then test for attributes.
    if ($node->nodeType==1)
    {
            /*print str_pad('',$node->depth,"_",STR_PAD_LEFT).$node->name.'/('.$parent.')<br>';*/
            print $parent.'<br>';
            $parent .= $node->name.'/';

    }
    //Call the recursive method again.
    parseXML($node,$parent);
    
}
$reader = new XMLReader();
$reader->open("test_data.xml");

parseXML($reader,"/");

?>
The function should allow to get from "any" XML all the xpath so they can be used in the database.

Thank you!!


feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
tim2
Forum Newbie
Posts: 2
Joined: Tue Jan 15, 2008 2:15 pm

Re: XML Parsing getting Xpath

Post by tim2 »

If you are using php 5.2 or later then you can use the undocumented getNodePath() method.

http://blog.liip.ch/archive/2006/07/15/ ... epath.html
maquejp
Forum Newbie
Posts: 2
Joined: Mon Jan 07, 2008 8:57 am

Re: XML Parsing getting Xpath

Post by maquejp »

looks what I looking for... Thank you!
Post Reply