How to change XML attribute value with PHP

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • darksteel21
    New Member
    • Jul 2008
    • 36

    How to change XML attribute value with PHP

    I have an xml file :
    Code:
    <?xml version="1.0"?>
    <profile id="4" sap="7778">
       <name="Mark"></name>
       <name="Mark"></name>
       <name="Mark"></name>
    </profile>
    I wan to modify the "id" attribute of the profile tag.
    How is it possible to php?
    Last edited by Dormilich; Jul 1 '10, 07:59 AM. Reason: Please use [code] tags when posting code
  • code green
    Recognized Expert Top Contributor
    • Mar 2007
    • 1726

    #2
    Using DOM you can remove and create attributes
    Code:
    DOMElement::removeAttribute ( string $name ) 
    DOMElement::setAttribute ( string $name , string $value )
    But DOM frightens a lot of fellow programmers. Managing the parent-child node hierarchy is tricky.
    I use a wrapper class with additional functions to move current node up and down levels.
    Then the current node becomes the class object
    PHP has a set of functions called simpleXML which seems to be the favourite, but only seems to have
    Code:
    addAttribute ( string $name , string $value [, string $namespace ] )

    Comment

    • gelmo214
      New Member
      • Jul 2010
      • 6

      #3
      Sir code green?

      this is really different from the topic above..sir?i have recently your post few years ago..but unfortunately i cant find that post again..it is regarding your solved post about updating multiple tables in one query..do you remember that sir?thanks for consideration sir..

      Comment

      • code green
        Recognized Expert Top Contributor
        • Mar 2007
        • 1726

        #4
        do you remember that sir
        If you are talking to me the answer is no.
        I suggest a new post.
        You may be told of for repeating a question that has already been answered,
        but hey, you should see some of the admonishments the experts get :)

        Comment

        • darksteel21
          New Member
          • Jul 2008
          • 36

          #5
          Originally posted by gelmo214
          this is really different from the topic above..sir?i have recently your post few years ago..but unfortunately i cant find that post again..it is regarding your solved post about updating multiple tables in one query..do you remember that sir?thanks for consideration sir..
          Yes.. i somehow do remember that post.. and it is really different from this post.. as far as i remember it is about mysql database while this one is about xml..

          Comment

          • darksteel21
            New Member
            • Jul 2008
            • 36

            #6
            Originally posted by code green
            Using DOM you can remove and create attributes
            Code:
            DOMElement::removeAttribute ( string $name ) 
            DOMElement::setAttribute ( string $name , string $value )
            But DOM frightens a lot of fellow programmers. Managing the parent-child node hierarchy is tricky.
            I use a wrapper class with additional functions to move current node up and down levels.
            Then the current node becomes the class object
            PHP has a set of functions called simpleXML which seems to be the favourite, but only seems to have
            Code:
            addAttribute ( string $name , string $value [, string $namespace ] )
            thanks for the information.. by d way i've already found a way in modifying the xml attributes using the simple_xml functions of php...

            Comment

            Working...