Rich Bradshaw
Rich Bradshaw

Reputation: 72965

When writing XML, is it better to hand write it, or to use a generator such as simpleXML in PHP?

I have normally hand written xml like this:

<tag><?= $value ?></tag>

Having found tools such as simpleXML, should I be using those instead? What's the advantage of doing it using a tool like that?

Upvotes: 5

Views: 2759

Answers (10)

Linto Finter
Linto Finter

Reputation:

Speed may be an issue... handwritten can be a lot faster.

Upvotes: 1

Hans
Hans

Reputation: 93

The XML tools in eclipse are really useful too. Just create a new xml schema and document, and you can easily use most of the graphical tools. I do like to point out that a prior understanding of how schemas work will be of use.

Upvotes: 0

Stefan Gehrig
Stefan Gehrig

Reputation: 83622

You could use the DOM extenstion which can be quite cumbersome to code against. My personal opinion is that the most effective way to write XML documents from ground up is the XMLWriter extension that comes with PHP and is enabled by default in recent versions.

$w=new XMLWriter();
$w->openMemory();
$w->startDocument('1.0','UTF-8');
$w->startElement("root");
    $w->writeAttribute("ah", "OK");
    $w->text('Wow, it works!');
$w->endElement();
echo htmlentities($w->outputMemory(true));

Upvotes: 3

Robert Rossney
Robert Rossney

Reputation: 96730

Producing XML via any sort of string manipulation opens the door for bugs to get into your code. The extremely simple example you posted, for instance, won't produce well-formed XML if $value contains an ampersand.

There aren't a lot of edge cases in XML, but there are enough that it's a waste of time to write your own code to handle them. (And if you don't handle them, your code will unexpectedly fail someday. Nobody wants that.) Any good XML tool will automatically handle those cases.

Upvotes: 2

hakamadare
hakamadare

Reputation: 1534

using a good XML generator will greatly reduce potential errors due to fat-fingering, lapse of attention, or whatever other human frailty. there are several different levels of machine assistance to choose from, however:

  1. at the very least, use a programmer's text editor that does syntax highlighting and auto-indentation. just noticing that your text is a different color than you expect, or not lining up the way you expect, can tip you off to a typo you might otherwise have missed.

  2. better yet, take a step back and write the XML as a data structure of whatever language you prefer, than convert that data structure to XML. Perl gives you modules such as the lightweight XML::Simple for small jobs or the heftier XML::Generator; using XML::Simple is just a matter of arranging your content into a standard Perl hash of hashes and running it through the appropriate method.

-steve

Upvotes: 2

FlySwat
FlySwat

Reputation: 175603

Always use a tool of some kind. XML can be very complex, I know that the PHP guys are used to working with hackey little stuff, but its a huge code smell in the .NET world if someone doesn't use System.XML for creating XML.

Upvotes: -1

Enrico Murru
Enrico Murru

Reputation: 2331

hand writing isn't always the best practice, because in large XML ou can write wrong tags and can be difficult to find the reason of an error. So I suggest to use XMl parsers to create XML files.

Upvotes: 1

Danimal
Danimal

Reputation: 7710

If you're dealing with a small bit of XML, there's little harm in doing it by hand (as long as you can avoid typos). However, with larger documents you're frequently better off using an editor, which can validate your doc against the schema and protect against typos.

Upvotes: 5

davetron5000
davetron5000

Reputation: 24841

Use the generator.

The advantage of using a generator is you have consistent markup and don't run the risk of fat-fingering a bracket or quote, or forgetting to encode something. This is crucial because these mistakes will not be found until runtime, unless you have significant tests to ensure otherwise.

Upvotes: 1

DGentry
DGentry

Reputation: 16248

Good XML tools will ensure that the resulting XML file properly validates against the DTD you are using.

Good XML tools also save a bunch of repetitive typing of tags.

Upvotes: 10

Related Questions