0

That's the task I'm trying to complete right now and I've seen a multitude of ways to do it with PHP in my Google searches and on Stack Overflow. Unfortunately, nothing seems to be working for me. I have no issues loading an existing .xml file with $xml = simplexml_load_file() and saving it to another directory on the server with $xml->asXML('example.xml'). But creating one is just not working.

This Stack Overflow page showed it with SimpleXML. I did what the edited answer for that question shows: $doc = new DOMDocument( '1.0' ); $doc->save("thexmlfile.xml"); but that didn't work.

This short video showed $xml = new DomDocument("1.0","UTF-8") and save it using $string_value = $xml_>saveXML(); $xml->save("example.xml") but that didn't work.

Here's my latest attempt. No XML file gets created as a result of this.

//Create an xml doc for the adcode folder
$xml = new DOMDocument("1.0","UTF-8");

$ticket = $xml->createElement("ticket");
$ticket = $xml->appendChild($ticket);

$ticketid = $xml->createElement('ticketid', $ticketid);
$ticketid = $ticket->appendChild($ticketid);


$xml->formatOutput = true;
$xml->asXML('code/' . $ticketid . '.xml');
Community
  • 1
  • 1
JoeL
  • 710
  • 4
  • 18
  • It looks like `asXML()` is a [SimpleXML method](https://php.net/manual/en/simplexmlelement.asxml.php), not a DOMDocument one. Do you have error reporting turned on? – halfer Mar 25 '15 at 22:54
  • Try `echo htmlentities($xml->saveXML())` at the end instead. I suggest using `htmlentities()` in case you're trying to view the output with a browser - don't forget you won't be able to with XML. – halfer Mar 25 '15 at 22:57
  • @halfer, why not just open the source view or send the `application/xml` content type? – ThW Mar 26 '15 at 09:57
  • @ThW - they'd be fine too. That said, I've been caught out several times thinking my tags have disappeared, only to find I've forgotten to View Source `:-)`. – halfer Mar 26 '15 at 10:01
  • You're missing to actually listening to PHP what it tells you about the errors it runs over: [How to get useful error messages in PHP?](http://stackoverflow.com/q/845021/367456). if something doesn't work, you should give a clear error-description including all error messages and warnings. A large set of error messages from PHP are explained per this reference: [Reference - What does this error mean in PHP?](http://stackoverflow.com/q/12769982/367456) – hakre Mar 26 '15 at 18:37
  • Hey thanks for the replies guys. I do have error reporting turned on but the only errors I were getting were later on when my .js file was trying to read the xmldoc and it said it didn't exist. I don't NEED to view it in a browser. The file saves to my server and I can just view the .xml file on my end that way as well. Viewing it in the browser is more convenient and the goal right now. – JoeL Mar 26 '15 at 19:23
  • I'm pretty sure that JS error reporting and PHP error reporting are a different pair of shoes. A call to `$xml->asXML()` triggers a fatal error in PHP for example. If you didn't see that one, well, a strong sign PHP error reporting is not set up properly for development. – hakre Mar 26 '15 at 19:27
  • Ok thanks. I actually just read your comment and clicked the links at this moment to read up on it. – JoeL Mar 26 '15 at 19:29
  • Thanks guys. i was able to figure it out, but I did it in a much simpler way that ended up making me bang my head on my desk for not noticing sooner. But I'm just a beginner coder so I'm assuming that there will plenty of those kinds of moments ahead. – JoeL Mar 27 '15 at 19:56
  • No problem, great to read you figured out already so much on your own. – hakre Mar 28 '15 at 00:13
  • That's how these things usually go. I get stumped, ask a question, read the responses but then figure it out a different way. It's a formula that works for me lol – JoeL Mar 28 '15 at 01:23

0 Answers0