xml editing

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
susrisha
Forum Contributor
Posts: 439
Joined: Thu Aug 07, 2008 11:43 pm
Location: Hyderabad India

xml editing

Post by susrisha »

Hello all..
I have an xml file of the format :

Code: Select all

 
<?xml version="1.0"?>
<annotations>
    
    <annotation id="3" type="HOTSPOT">
        <time>
            <starttime>7000</starttime>
            <endtime>15000 </endtime>
        </time>
        <params>
            <x>100</x>
            <y>100</y>
            <width>50</width>
            <height>100</height>
            <text>Sample HotSpot</text>
        </params>
    </annotation>   
 
    <annotation id="4" type="HOTSPOT">
        <time>
            <starttime>12000</starttime>
            <endtime>14000 </endtime>
        </time>
        <params>
            <x>220</x>
            <y>70</y>
            <width>50</width>
            <height>80</height>
            <text>Sample HotSpot</text>
        </params>
    </annotation>
 
    <annotation id="1" type="NOTE">
        <time>
            <starttime>13000</starttime>
            <endtime>25000</endtime>
        </time>
        <params>
            <x>170</x>
            <y>160</y>
            <width>100</width>
            <height>40</height>
            <text>Sample Note using xml</text>
        </params>
    </annotation>
        
    <annotation id="2" type="SPEECHBUBBLE">
        <time>
            <starttime>4000</starttime>
            <endtime> 6000</endtime>
        </time>
        <params>
            <x>20</x>
            <y>40</y>
            <width>100</width>
            <height>60</height>
            <text>Sample speech bubble using xml</text>
            <px>180</px>
            <py>80</py>         
        </params>
    </annotation>
    <annotation id="0" type="PAUSE">
        <time>
            <starttime>8000</starttime>
            <endtime>8000</endtime>
            <pausetime>10000</pausetime>
        </time>
        <params>
            <x>20</x>
            <y>40</y>
            <width>100</width>
            <height>60</height>
                
        </params>
    </annotation>
 
    
 
</annotations>
 
There is another file which i got

Code: Select all

 
<document>
<requestHeader video_id="xyz" />
<updateitems>
       <annotation id="3" type="HOTSPOT">
        <time>
            <starttime>7000</starttime>
            <endtime>15000 </endtime>
        </time>
        <params>
            <x>150</x>
            <y>125</y>
            <width>30</width>
            <height>10</height>
            <text>Sample HotSpot</text>
        </params>
    </annotation>
</updateitems>
</document>
 
My problem is how do i replace the parameters of the respective annotation in first page to the annotations in the second xml file.?
User avatar
novice4eva
Forum Contributor
Posts: 327
Joined: Thu Mar 29, 2007 3:48 am
Location: Nepal

Re: xml editing

Post by novice4eva »

Take a look at DOM functions in PHP
User avatar
susrisha
Forum Contributor
Posts: 439
Joined: Thu Aug 07, 2008 11:43 pm
Location: Hyderabad India

Re: xml editing

Post by susrisha »

Well i suppose that requires a PECL extension but i wanted it to be done on simplexml. is there anyway i can do it?
User avatar
novice4eva
Forum Contributor
Posts: 327
Joined: Thu Mar 29, 2007 3:48 am
Location: Nepal

Re: xml editing

Post by novice4eva »

I have only worked with DOM, which i always thought was a better way, now you've asked it, i did just go through manual and it should be possible, simple xml does provide functions of addChild and addAttribute but i couldn't find anything that directly writes to the XML file itself, i think you will have to re-create the XML text and later add it to a file.

DOM does come bundled with PHP5 and i have no clew regarding its association with PECL 8O
User avatar
susrisha
Forum Contributor
Posts: 439
Joined: Thu Aug 07, 2008 11:43 pm
Location: Hyderabad India

Re: xml editing

Post by susrisha »

i went through the documentation and found the solution. Now i can add the elements and nodes to the xml file. simpleXML is able to do it and i am just saving it as the same file name to overwrite the content and add the given elements. There is also another problem of deleting a particular <annotation> node given the parameter id. I used DOMDOCUMENT class for removing and simpleXML class for adding. Did it finally though. Thanks for the help
Post Reply