in reply to Outputting a hash as XML

just use print();

I mean you have a data-structure and want it to become XML. So create a function that will produce XML. It may be extra typing than using a module like XML::Twig, but it's the most reliable way. If you want to be smart, make your data-structure a class and add a toXML-method to it.



holli, /regexed monk/

Replies are listed 'Best First'.
Re^2: Outputting a hash as XML
by Tanktalus (Canon) on Mar 16, 2005 at 22:26 UTC

    I have to wonder if mirod would take offense at implying that XML::Twig is less reliable than other choices ;-)

    For something this small, XML::Twig may be overkill. But for the project we're working on at work, "just us[ing] print()" is actually less reliable. Finding and quashing close-tag errors in a multi-MB XML file is painful. Using a module, such as XML::Twig, helps to avoid these errors.

    And writing a "toXML" function is exactly the same problem that the OP had - just not objectified. For these, I, personally, would still want to rely on an XML module to handle things for me.

    Just my opinion... :-)

Re^2: Outputting a hash as XML
by mirod (Canon) on Mar 16, 2005 at 21:25 UTC

    In this case you have to make sure you escape properly & and < at least (plus " in attribute values if you have attributes and use " to quote them). Writing XML is a lot simpler than parsing it, but I still would hesitate to recommend using print to a newcomer. There is enough broken XML floating around, no need to risk adding some more.

    Oh, and of course you need to make sure that the encoding of your data is OK (pure ascii or utf-8) or you need to add an XML declaration with the proper encoding. And most modules won't help you much there.