3

I have an xml file

<?xml version="1.0" encoding="UTF-8"?>
<xml>
<settings>
<title>Calendar2</title>
<subTitle>Calendar2</subTitle>
</settings>
<events date="02-09-2010">
<event>
<title>HTML Tags</title>
<description>HTML Tags</description>
</event>
</events>
</xml>

How i can add another event inside events tag with respect to date

<?xml version="1.0" encoding="UTF-8"?>
    <xml>
    <settings>
    <title>Calendar2</title>
    <subTitle>Calendar2</subTitle>
    </settings>
    <events date="02-09-2010">
    <event>
    <title>HTML Tags</title>
    <description>HTML Tags</description>
    </event>
    <event>
    <title>Another Title</title>
    <description>Another description</description>
    </event>
    </events>
    </xml>

i used this code

$xml_str = file_get_contents($xmlfile);
$xml = new SimpleXMLElement($xml_str);
$event = $xml->events->addChild('event');
$event->addChild('title', 'More Parser Stories');
$event->addChild('description', 'This is all about the people who make it work.');
file_put_contents($xmlfile, $xml->asXML());

But it will add to the first node.How i can add to events tag with date 02-09-2010

1
  • This seems to be the same as your previous question: Editing xml file . You should edit and update your original question instead of creating a new one. Commented Sep 2, 2010 at 7:02

2 Answers 2

1

You'll have to query for the wanted <events> tag instead of taking the first one (which what $xml->events would simply return), using xpath to query the xml document is helpful here:

PHP Script:

<?php
$xml_str = file_get_contents('xmlfile');
$xml = new SimpleXMLElement($xml_str);
$wantedEventsTag = $xml->xpath('/xml/events[@date="02-09-2010"]');
$wantedEventsTag = $wantedEventsTag [0];//since above fun will return an array
$wantedEventsTag['attrname']='attrval';//Here's how to add an attribute
$event = $wantedEventsTag->addChild('event');
$event->addChild('title', 'More Parser Stories');
$event->addChild('description', 'This is all about the people who make it work.');
file_put_contents('xmlfile.xml', $xml->asXML());

Sample XML File with multiple <events> tags:

<?xml version="1.0" encoding="UTF-8"?>
<xml>
  <settings>
    <title>Calendar2</title>
    <subTitle>Calendar2</subTitle>
  </settings>
  <events date="01-01-1999">
  </events>
  <events>
  </events>
  <events date="02-09-2010">
    <event>
      <title>HTML Tags</title>
      <description>HTML Tags</description>
    </event>
    <event>
      <title>Another Title</title>
      <description>Another description</description>
    </event>
  </events>
</xml>

the script xpath will match the required node, which we later use and add events subnodes to.

Sign up to request clarification or add additional context in comments.

2 Comments

How i can add a attribute to event tag
I edited my answer above to contain a line on adding attributes (commented as doing so).
0

You'll need to use DOM instead, specifically DOMNode::insertBefore:

http://us.php.net/manual/en/domnode.insertbefore.php

1 Comment

Is any other way like xpath or xquery

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.