2

i already search here before i ask this question.. I know there is an answer here somewhere.. but i can't find it..

I want to be able to edit child node by id in my xml file.. i tried alot of script but i'm having a really bad time with this..

please see this line in edit.php:

echo "the problem is in the next line";

this is the last line that the server reads..

thanks you for your help.

events.xml

<?xml version="1.0" encoding="UTF-8" ?>
<events>
    <record>
        <id>1</id>
        <event>a</event>
        <eventDate>a</eventDate>
        <desc>a</desc>
    </record>
    <record>
        <id>2</id>
        <event>b</event>
        <eventDate>b</eventDate>
        <desc>b</desc>
    </record>
</events>

edit.php

header("Content-type: text/html; charset=utf-8");

$record = array(
    'id' => $_POST['id'],
    'event' => $_POST['event'],
    'eventDate' => $_POST['eventDate'],
    'desc' => $_POST['desc'],
);

$id = $record["id"];
$dom = new DOMDocument;
$dom->load('events.xml');

$xpath = new DOMXPath($dom);
$query = sprintf('/events/record[./id = "%d"]', $id);

foreach($xpath->query($query) as $record) {

    $eventN = $record->parentNode->getElementsByTagName("event");
    echo "the problem is in the next line";
    $eventN->item(0)->nodeValue = $record["event"];

    $dateN = $record->parentNode->getElementsByTagName("eventDate");
    $dateN->item(0)->nodeValue = $record["eventDate"];

    $descN = $record->parentNode->getElementsByTagName("desc");
    $descN->item(0)->nodeValue = $record["desc"];

}
$dom->save("events.xml");
header("Location: {$_SERVER['HTTP_REFERER']}");
?>

Edited: Working edit.php but not dynamic

<?php
header("Content-type: text/html; charset=utf-8");

$record = array(
    'id' => $_POST['id'],
    'event' => $_POST['event'],
    'eventDate' => $_POST['eventDate'],
    'desc' => $_POST['desc'],
);

$id = $record["id"];
$dom = new DOMDocument;
$dom->load('events.xml');

$xpath = new DOMXPath($dom);
$query = sprintf('/events/record[./id = "%d"]', $id);

foreach($xpath->query($query) as $record) {

    $eventN = $record->parentNode->getElementsByTagName("event");
    echo "i change it to string text and it's works. ";
    $eventN->item(0)->nodeValue = 'text';

    $dateN = $record->parentNode->getElementsByTagName("eventDate");
    $dateN->item(0)->nodeValue = 'text';

    $descN = $record->parentNode->getElementsByTagName("desc");
    $descN->item(0)->nodeValue = 'text';

}
$dom->save("events.xml");
header("Location: {$_SERVER['HTTP_REFERER']}");
?>
8
  • you say that it's the last line the server reads. does it give any errors? Commented Jan 12, 2011 at 13:26
  • Fatal error: Cannot use object of type DOMElement as array in /~/www/ofear.onlinewebshop.net/asce/edit.php on line 24 Commented Jan 12, 2011 at 13:33
  • the edit.php supposed to get data from a POST data and to search for a specific id and change it's child nodes... Commented Jan 12, 2011 at 13:38
  • well..the problem is that the $record["event"] can't work.. but if i doing this for example '$record["event"]' <- like a string , then it's works and edit the childes Commented Jan 12, 2011 at 13:44
  • how can i make the $record["event"] to be a string? Commented Jan 12, 2011 at 13:48

1 Answer 1

6

Ok, the issue is your $record array from the beginning is overwritten in the foreach.

alt text

Either change the as $record or change the name of the array holding the $_POST data.

On a sidenote, you are iterating over elements, so there is no reason to get to the parentNode. Use

$record->getElementsByTagName("event");
Sign up to request clarification or add additional context in comments.

Comments

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.