1

I'm trying to write on xml file. but my server have DOM as disabled. so i can't use the DOM function.

is there a way to write on xml other then DOM?

Files:

xml:

<?xml version="1.0" encoding="UTF-8"?>
<events>
<record>
<event>aaa</event>
<eventDate>bbb</eventDate>
<desc>ccc</desc>
</record>

parser.php:

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

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

$doc = new DOMDocument();
$doc->load( 'calendar.xml' );

$doc->formatOutput = true;
$r = $doc->getElementsByTagName("events")->item(0);

$b = $doc->createElement("record");

$event = $doc->createElement("event");
$event->appendChild(
    $doc->createTextNode( $record["event"] )
);
$b->appendChild( $event );

$eventDate = $doc->createElement("eventDate");
$eventDate->appendChild(
    $doc->createTextNode( $record["eventDate"] )
);
$b->appendChild( $eventDate );

$desc = $doc->createElement("desc");
$desc->appendChild(
    $doc->createTextNode( $record["desc"] )
);

$b->appendChild( $desc );
$r->insertBefore( $b,$r->firstChild );

$doc->save("calendar.xml");

header("Location: {$_SERVER['HTTP_REFERER']}");    

what can i do?

ajreal
  • 46,720
  • 11
  • 89
  • 119
Ofir Hadad
  • 1,800
  • 3
  • 26
  • 47
  • 1
    DOM is enabled by default in PHP5. If your host disabled it, ask them why because there is no reason to do so. Better yet, switch providers. – Gordon Jan 10 '11 at 13:41

2 Answers2

1

if you're using PHP5 you can use SimpleXML

Peng Qi
  • 1,412
  • 1
  • 10
  • 13
0

You could edit the file as if it were a plain text file. However, you will need to make sure all elements are closed and all data is escaped correctly. If you only need to output XML this is doable, but to write a parser that can add to and modify existing XML data is a tough job. Better try to enable DOM or find an out of the box library.

GolezTrol
  • 114,394
  • 18
  • 182
  • 210