ellis has asked for the wisdom of the Perl Monks concerning the following question:

I am using the cpan XML-DOM-Lite library version 0.08 to create an xml document. The following is the code and output.

my $dc = XML::DOM::Lite::Document->new(); my $root = $dc->createElement("xml"); my $elem = $dc->createElement("A"); $elem->setAttribute("B", "C"); $root->appendChild($elem); $dc->appendChild($root); print $dc->xml(); Output: ======= <xml> <A B="C" /> </xml>

Instead of having "B" defined as an attribute, I want "B" to be defined as a separate tagname, with "C" defined as a text. Is this xml output possible?

Desired output: =============== <xml> <A><B> C </B></A> </xml>

I made the following changes to the code, and it returned an error message.

my $dc = XML::DOM::Lite::Document->new(); my $root = $dc->createElement("xml"); my $elem = $dc->createElement("A"); my $elem2 = $dc->createElement("B"); # Added $elem2->createTextNode("C"); # Added $elem->appendChild($elem2); # Added $root->appendChild($elem); $dc->appendChild($root);

Error message: Can't locate object method "createTextNode" via package "XML::DOM::Lite::Node"

Replies are listed 'Best First'.
Re: Question about XML::DOM::Lite
by NetWallah (Canon) on Aug 16, 2011 at 20:34 UTC
    createTextNode is a DOCUMENT method, so the object used should be $dc, not $elem2.

    It returns a NODE object that you can use to attach as a child node.

                "XML is like violence: if it doesn't solve your problem, use more."

Re: Question about XML::DOM::Lite
by choroba (Cardinal) on Aug 19, 2011 at 14:16 UTC
    I usually use XML::XSH2 for XML manipulation:
    create xml ; insert element A into xml ; insert element B into xml/A ; cd xml/A/B ; insert text "C" into . ;