Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New Coder
    Join Date
    Feb 2010
    Posts
    95
    Thanks
    14
    Thanked 0 Times in 0 Posts

    How to access variable in xml

    Hi, I have the following xml-file (lang.xml):

    Code:
    <lang>
      <langName>English</langName>
      <months>
          <month num = "1">Jan</month>
          <month num = "2">Feb</month>
      </months>
    </lang>
    To access the langName-variable I just write:
    PHP Code:
        $xml simplexml_load_file("lang.xml");
        echo 
    $xml->langName
    but how do I access the value "Feb" ($xml->months->month...), it must be possible to include the number "2" somehow in the php-code

  • #2
    Senior Coder Dormilich's Avatar
    Join Date
    Jan 2010
    Location
    Behind the Wall
    Posts
    3,133
    Thanks
    12
    Thanked 332 Times in 328 Posts
    Quote Originally Posted by earni23 View Post
    To access the langName-variable I just write:
    the correct term here is node, XML is not a programming language, thus there are no variables.

    Quote Originally Posted by earni23 View Post
    but how do I access the value "Feb" ($xml->months->month...), it must be possible to include the number "2" somehow in the php-code
    of course this is possible. you can access it by its list order number $xml->months->month[1] or if you explicitly test each list item’s num attribute.
    The computer is always right. The computer is always right. The computer is always right. Take it from someone who has programmed for over ten years: not once has the computational mechanism of the machine malfunctioned.
    André Behrens, NY Times Software Developer

  • Users who have thanked Dormilich for this post:

    earni23 (03-23-2010)


  •  

    Posting Permissions

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