All in the <head>

– Ponderings & code by Drew McLellan –

– Live from The Internets since 2003 –

About

More on XML

16 October 2003

If you’ve not been following the whole Atom thing, then you should really read Mark’s excellent introduction to Atom. He does an excellent job of putting you in the picture without getting bogged down with any particular aspect.

On a similar XML note (la!) our old pals the W3C have gone and made XForms an official Recommendation. Hurrah! I know you’re thinking what the hell use is that when no browsers support XForms. The important factor when a technology becomes an official recommendation is that the W3C are now saying you should do forms like this which gives browser manufacturers the authority and confidence to actually start implementing that technology. In days of yore it would be about three years (literally) before such a technology would become useable. That may still be so, but I rather get the impression that with the development models surrounding modern browsers that we might see new technologies like XForms becoming viable for use more and more quickly after recommendation. Sure, it’s still going to take a whole chunk of time to code support for brand new technologies, but detachment from corporate bureaucracy and schedules could well mean new technologies have a much quicker route to market.

I’m still having problems with XML in PHP. I can find lots of libraries that help me read XML files, but none that help me write them. With PHP, what’s the easiest way to add a node into the middle of a document without a DOM? Any ideas?

- Drew McLellan

Comments

  1. § Dysfunksional Monkey: If you know what the previous-sibling node is, do a regexp to replace it with itself plus the extra node. Or even use str_replace() if you know the node and its exact content.
  2. § Nathan Pitman: Clever Monkey. :)
  3. § Drew: That’s a good point. I don’t know the previous sibling, but I do know the next, so that would work.

    A bit dirty, but it’d work.
  4. § Dysfunksional Monkey: Yep. How much do you know about the next node?

    Consider the following XML file:

    <?xml version="1.0"?>
    <books>
     <book name="My Way">
      <author name="bob" />
     </book>
     <book name="His Way">
      <author name="terry" />
     </book>
    </books>

    If you wanted to insert a new co-writer for ”His Way”, and knew the whole next-/previous-sibling, you would use:
    <?php //ALWAYS use this opening tag when working with PHP. Otherwise inline XML will break

    $previous-sibling = ’’;
    $self = ’<author name="terry" />’;
    $next-sibling = ’<author name="oswald" />’;

    $xml = str_replace($self,$previous-sibling.$self.$next-sibling,$xml);

    ?>


    You might need to add newline and tab chars in there for pretty formatting.

    Now, if you only know part of the node you want to insert-after/-before/replace, you use a regexp for the node name and something else which you know, like the ID. This is a bit slower, but works.

    Personally, I tend not to do anything to the XML from within PHP. I use an XMLDB, and use XUpdate for that. Its so much simpler, especially when you have an XUpdate class to do the work for you. :)
  5. § Drew: In this case my XML file would be (or could be designed to be) of a very simple format.

    Thanks for the example.

    It seems daft to manipulate XML like an unstructured text file, but if it gets the job done then it’s probably ok. I’ll still left with the advantage of being able to style it with XSLT.
  6. § Drew: I was pointed in the direction of Active Link today. Has anyone got experience of using their libraries? I’m gunna give them a go.

    (The link might be useful for those arriving here via Google!)
  7. § Gabe: Have you looked at PEAR’s XML classes? One that pops out is http://pear.php.net/package/XML_Serializer , not sure if it is practical, but I’ve found PEAR to be a great place to start for almost anything even if I’ve had to do a little tweaking myself.
  8. § zlog: Did you get my email? I think it may be what you are looking for.
  9. § Drew: Yes, thanks Ronan! I’ve haven’t tried it yet, but it looks like just the thing. (I also have a big backlog of email).
  10. § Dysfunksional Monkey: Zlog - was it something to do with XML? If so, could you post it on here? It would be nice for the other users (including myself) to have a look.

Photographs

Work With Me

edgeofmyseat.com logo

At edgeofmyseat.com we build custom content management systems, ecommerce solutions and develop web apps.

Recent Links

Affiliation

  • Web Standards Project
  • Britpack
  • 24 ways

About Drew McLellan

Photo of Drew McLellan

Drew McLellan has been hacking on the web since around 1996 following an unfortunate incident with a margarine tub. Since then he’s spread himself between both front- and back-end development projects, and now is Director and Senior Web Developer at edgeofmyseat.com in Maidenhead, UK (GEO: 51.5217, -0.7177). Prior to this, Drew was a Web Developer for Yahoo!, and before that primarily worked as a technical lead within design and branding agencies for clients such as Nissan, Goodyear Dunlop, Siemens/Bosch, Cadburys, ICI Dulux and Virgin.net. Somewhere along the way, Drew managed to get himself embroiled with Dreamweaver and was made an early Macromedia Evangelist for that product. This lead to book deals, public appearances, fame, glory, and his eventual downfall.

Picking himself up again, Drew is now a strong advocate for best practises, and stood as Group Lead for The Web Standards Project 2006-08. He has had articles published by A List Apart, Adobe, and O’Reilly Media’s XML.com, mostly due to mistaken identity. Drew is a proponent of the lower-case semantic web, and is currently expending energies in the direction of the microformats movement, with particular interests in making parsers an off-the-shelf commodity and developing simple UI conventions. He writes here at all in the head and, with a little help from his friends, at 24 ways.