read and edit xml with PHP

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • John Gauger

    read and edit xml with PHP

    Hi all,
    Thanks in advance for any help that you can give. I'm trying to write
    some php that will read in some xml and then allow me to modify the tag

    and write back out to xml.


    In the following example, I want to read in 'text_for_parag raph_1',
    then allow it to be viewed, edited, and saved back to the xml... I
    have absolutely no clue even where to start...any help at all would be
    more than appreciated!!


    thanks,
    john


    <?xml version="1.0" standalone="yes "?>
    <module name="GS02" course="Demo">
    <page>
    <Name><![CDATA[ c0300010 (Ref Num 772) ]]></Name>

    <Screen_Type> <![CDATA[Text_wit­h_Imag e]]></Screen_Type>
    <Description> <![CDATA[ &nbsp; ]]></Description>
    <Script>
    <Text_for_Parag raph_1><![CDATA­[ Welcome to
    lesson 1 of module 2.
    &nbsp; In this lesson, you will learn about the emerging trends in the
    IT marketplace. ]]></Text_for_Paragr aph_1>
    </Script>
    </page>
    </module

  • Oli Filth

    #2
    Re: read and edit xml with PHP

    John Gauger wrote:[color=blue]
    > Hi all,
    > Thanks in advance for any help that you can give. I'm trying to[/color]
    write[color=blue]
    > some php that will read in some xml and then allow me to modify the[/color]
    tag[color=blue]
    >
    > and write back out to xml.
    >
    >
    > In the following example, I want to read in 'text_for_parag raph_1',
    > then allow it to be viewed, edited, and saved back to the xml... I
    > have absolutely no clue even where to start...any help at all would[/color]
    be[color=blue]
    > more than appreciated!!
    >
    >
    > thanks,
    > john
    >
    >
    > <?xml version="1.0" standalone="yes "?>
    > <module name="GS02" course="Demo">
    > <page>
    > <Name><![CDATA[ c0300010 (Ref Num 772) ]]></Name>
    >
    > <Screen_Type> <![CDATA[Text_wit­h_Imag e]]></Screen_Type>
    > <Description> <![CDATA[ &nbsp; ]]></Description>
    > <Script>
    > <Text_for_Parag raph_1><![CDATA­[ Welcome to
    > lesson 1 of module 2.
    > &nbsp; In this lesson, you will learn about the emerging trends in[/color]
    the[color=blue]
    > IT marketplace. ]]></Text_for_Paragr aph_1>
    > </Script>
    > </page>
    > </module[/color]

    Some possible approaches (visit the PHP manual at http://php.net/manual
    for more details on these functions, etc.):

    * preg_match() and preg_replace(). Devise a regular expression that
    gives a hit on <Text_for_Parag raph_1>...</Text_for_Paragr aph_1>.

    * Use an XSLT transform (see the PHP XSLT functions).

    * Load the whole script in using the XML DOM functions, and the find
    and replace the contents of the relevant element.

    * Save the XML file as some sort of template, with something like
    {PARAGRAPH_TEXT } in the right place. Then do a preg_replace() on this.


    P.S. Are you aware that you don't have to escape *all* character data
    with <![CDATA[ ... ]]>? This is only necessary if you want to express
    data that would otherwise look like XML.

    --
    Oli

    Comment

    • teemu.valimaki@gmail.com

      #3
      Re: read and edit xml with PHP

      Hi John

      There's probably more than one library made for PHP that takes care of
      XML reading/writing, one is below.



      Comment

      • Teemu Välimäki

        #4
        Re: read and edit xml with PHP



        seems to be even better.

        Comment

        Working...