SitePoint Sponsor

User Tag List

Results 1 to 3 of 3

Thread: Navigating to LastChild in XML

  1. #1
    SitePoint Member
    Join Date
    Jan 2008
    Posts
    20
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question Navigating to LastChild in XML

    Hey Guys,

    I'm trying to find a way to get to <key k_id="4"> using php. I've been trying to get this working all day yesterday and today. Can you guys see what I'm doing wrong?

    This is my xml file
    Code:
    <?xml version="1.0" encoding="utf-8"?>
    <keys>
      <key k_id="1">
        <k_id>1</k_id>
        <k_key>_Monkey</k_key>
      </key>
      <key k_id="2">
        <k_id>2</k_id>
        <k_key>_Test</k_key>
      </key>
      <key k_id="3">
        <k_id>3</k_id>
        <k_key>_Hello</k_key>
      </key>
      <key k_id="4">
        <k_id>4</k_id>
        <k_key>_GoodBye</k_key>
      </key>
    </keys>

    The php code I'm using that's not working.
    Code:
    $doc = new DOMDocument();
    $doc->formatOutput = true;
    $doc->load("{$this->paths->avs_web}xml/keys.xml");
    
    foreach ($doc->childNodes as $node) {
    	$lastelement = $this->get_lastchild($node->lastChild);
    	$lastid = (int)$lastelement->getAttribute("k_id");
    	$newid = $lastid + 1;
    	
    }
    GamePacks.org - Products for the Arcade/Media Community
    Video Script - The ultimate media site script
    AutoVideoScript.com - Run your own youtube sharing site

  2. #2
    Guru in training bronze trophy SoulScratch's Avatar
    Join Date
    Apr 2006
    Location
    Maryland
    Posts
    1,838
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    can you not use the getElementById method and feed that as a string?
    Cross browser css bugs

    Dan Schulz you will be missed

  3. #3
    Twitter: @AnthonySterling silver trophy AnthonySterling's Avatar
    Join Date
    Apr 2008
    Location
    North-East, UK.
    Posts
    6,109
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    Here you, this works for me.
    PHP Code:
    <?php
    $sXML 
    '
    <keys>
      <key k_id="1">
        <k_id>1</k_id>
        <k_key>_Monkey</k_key>
      </key>
      <key k_id="2">
        <k_id>2</k_id>
        <k_key>_Test</k_key>
      </key>
      <key k_id="3">
        <k_id>3</k_id>
        <k_key>_Hello</k_key>
      </key>
      <key k_id="4">
        <k_id>4</k_id>
        <k_key>_GoodBye</k_key>
      </key>
    </keys>
    '
    ;
    $oXPath = new DOMXPath(DOMDocument::loadXML($sXML));
    $oFinalNode $oXPath->query('/keys/*[last()]');
    echo 
    $oFinalNode->item(0)->getAttribute('k_id'); #4
    ?>
    @AnthonySterling: I'm a PHP developer, a consultant for oopnorth.com and the organiser of @phpne, a PHP User Group covering the North-East of England.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •