Jump to content

Php and XML


HardCoreMore

Recommended Posts

Hi, i have problem with controlling XML file so please send me link if you know the post that my solve my problem.

 

This is the structure of xml that i need.

<content>
    <user id="12">
      <pic>some value</pic>
      <pic>some value</pic>
      <pic>some value</pic>
       </user>
     <user id="28">
      <pic>some value</pic>
      <pic>some value</pic>
      <pic>some value</pic>
      <pic>some value</pic>
     </user>
     <user id="54">
      <pic>some value</pic>
      <pic>some value</pic>
     </user>
        </content>

 

So how can i open this xml and find all children of user tag where id in user tag has value of 54 for example so that i know how many pic tags are there cauz value inside pic is going to be picture name for certain user and i want to limit number of pic tags so that user can only upload 20 pictures.

How can i add new user tag to this xml? How can i add and delete new pic tag inside user tag where id is 28 for example.

I searched the php manual and i found DomDocument functions for editing xml but they are so unclear to me. I really don't know how to use them.

Only i know by far is to open xml  with domxml_open_file()

I hope that you understand what i want to achieve. Any help is welcome.

Link to comment
https://forums.phpfreaks.com/topic/126880-php-and-xml/
Share on other sites

I was reading some tutorials on how to parse and edit xml. I found that best solution is to use simple xml and dom in combination but i still have one problem.

I know how to add an element but i don't know how to add and attribute at the same time when i am adding new element. So i am using this code to add a new element.

$book = simplexml_load_file('book.xml');
$daj = $book->user[0];
$dom = dom_import_simplexml($daj);
$dom->appendChild(new DOMElement("pic", "New Value"));

 

So when i add this new element to the xml how can i add and attribute to that element with some value.

Link to comment
https://forums.phpfreaks.com/topic/126880-php-and-xml/#findComment-659867
Share on other sites

If i change code to this

$book = simplexml_load_file('book.xml');
$daj = $book->user[0];
$dom = dom_import_simplexml($daj);
$dom->appendChild(new DOMElement("pic", "New Value",[color=red]"attValue"[/color]));

He makes node and makes attribute with value attValue but i can't choose the name of the attribute?

Instead he makes automatically name "xmlns". Why is that happening?

Link to comment
https://forums.phpfreaks.com/topic/126880-php-and-xml/#findComment-659879
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.