xml attributes and xml element count

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
User avatar
hannnndy
Forum Contributor
Posts: 131
Joined: Sat Jan 12, 2008 2:09 am
Location: Iran>Tehran
Contact:

xml attributes and xml element count

Post by hannnndy »

Hi Friends

i have this xml file

Code: Select all

 
<?xml version="1.0"?>
<services>
    <service id="1">
        <title>????? ???? ?? ?????</title>
        <language>fa</language>
        <description></description>
    </service>
    <service id="2">
        <title>????? ???????? ??</title>
        <language>fa</language>
        <description></description>
    </service>
    <service id="3">
        <title>????? ???????? ?????? ?????</title>
        <language>fa</language>
        <description></description>
    </service>
    <service id="4">
        <title>?????? ??????</title>
        <language>fa</language>
        <description></description>
    </service>
    <service id="5">
        <title>Google Services in Iran parsitech</title>
        <language>en</language>
        <description></description>
    </service>
    <service id="6">
        <title>Web Development Department of parsitech</title>
        <language>en</language>
        <description></description>
    </service>
    <service id="7">
        <title>Programming Department of parsitech</title>
        <language>en</language>
        <description></description>
    </service>
    <service id="8">
        <title>e-learning Components in parsitech</title>
        <language>en</language>
        <description></description>
    </service>
</services>
 
1.I want to know how can i return "8" the id of the latest node in order to add a new element?
2.how can i find out how many elements in the services node do i have

Code: Select all

 
    public function retInfo()
    {
        if($this->__xmlFileName_str == "") return false;
        $xpath = new DomXPath($this->__domContent_xml);
        $this->title = $xpath->query('/services/service/title')->item(0)->nodeValue; // selecting the title via xpath
        $this->language = $xpath->query('/services/service/language')->item(0)->nodeValue;
        $this->description = $xpath->query('/services/service/description')->item(0)->nodeValue;
        //echo($this->title);
        return true;
    }   //  retInfo()
 
this code only returns the first node
thanks for your help
sylsft
Forum Newbie
Posts: 2
Joined: Mon May 26, 2008 5:36 pm
Location: Waterloo, Ontario, Canada

Re: xml attributes and xml element count

Post by sylsft »

hannnndy wrote: this code only returns the first node
thanks for your help
Have a look at the docs for the query() function:
http://ca3.php.net/manual/en/domxpath.query.php

It returns a DOMNodeList:
http://ca3.php.net/manual/en/class.domnodelist.php

So you can use the length variable to figure out how long the array of items is.

Hope that helps!
Post Reply