how to $doc->createElement with XML::LibXML

Discussion in 'Perl Misc' started by zacky az, Feb 22, 2010.

  1. zacky az

    zacky az Guest

    Hi,

    i am trying to create a xml file with the following

    <?xml version="1.0" encoding="UTF-8"?>
    <file name="file 1"/>
    <file name="file 2"/>

    i try the following code without any success, can someone help me to
    understand what i am dong wrong?

    use strict;
    use XML::LibXML;

    my $doc = XML::LibXML::Document->new( '1.0', 'UTF-8' );
    my $root = $doc->createElementNS('', 'file');
    my $attr = $doc->createAttributeNS( "", "name", "file 1" );
    $root->setAttributeNodeNS( $attr );
    $doc->setDocumentElement($root);
    print "\n\n ";
    print $doc->toString(2);
    print "\n\n ";

    my $root2 = $doc->createElementNS('', 'file');
    my $attr2 = $doc->createAttributeNS( "", "name", "file 2" );
    $root2->setAttributeNodeNS( $attr2 );
    $doc->setDocumentElement($root2);
    print "\n\n ";
    print $doc->toString(2);
    print "\n\n ";

    the result is

    <?xml version="1.0" encoding="UTF-8"?>
    <file name="file 1"/>


    <?xml version="1.0" encoding="UTF-8"?>
    <file name="file 2"/>

    Regards Zacky
     
    zacky az, Feb 22, 2010
    #1
    1. Advertising

  2. zacky az

    zacky az Guest

    On Feb 22, 10:59 pm, Ben Morrow <> wrote:
    > Quoth zacky az <>:
    >
    >
    >
    > > i am trying to create a xml file with the following

    >
    > >  <?xml version="1.0" encoding="UTF-8"?>
    > > <file name="file 1"/>
    > >  <file name="file 2"/>

    >
    > This is not valid XML. An XML document has a *single* root element
    > containing all the other elements; this has two <file> elements at the
    > root.
    >
    > What makes you think you need the XML to look like this?
    >
    > Ben


    Hi,

    Thank, a pmd process is creating an xml like this and i see my problem
    the pmd in the root

    <?xml version="1.0" encoding="UTF-8"?>
    <pmd version="4.2.5" timestamp="2010-02-22T18:24:22.007">
    <file name="ContractUploadProcessor.java">
    <violation beginline="3" endline="3" begincolumn="1" endcolumn="20"
    rule="UnusedImports" ruleset="Import Statement Rules"
    package="processors" externalInfoUrl="http://pmd.sourceforge.net/rules/
    imports.html#UnusedImports" priority="4">
    Avoid unused imports such as 'java.io.File'
    </violation>
    <file name="ContractCsvEngine.java">
    <violation beginline="26" endline="26" begincolumn="1" endcolumn="54"
    rule="UnusedImports" ruleset="Import Statement Rules"
    package="imports" externalInfoUrl="http://pmd.sourceforge.net/rules/
    imports.html#UnusedImports" priority="4">
    Avoid unused imports such as 'ataImportActionBo'
    </violation>
    </file>
    </pmd>

    thank and Regards Zacky
     
    zacky az, Feb 22, 2010
    #2
    1. Advertising

  3. zacky az

    Guest

    On Mon, 22 Feb 2010 12:43:31 -0800 (PST), zacky az <> wrote:

    >Hi,
    >
    >i am trying to create a xml file with the following
    >
    > <?xml version="1.0" encoding="UTF-8"?>
    ><file name="file 1"/>
    > <file name="file 2"/>
    >
    >i try the following code without any success, can someone help me to
    >understand what i am dong wrong?


    [snip]
    >
    > <?xml version="1.0" encoding="UTF-8"?>
    ><file name="file 1"/>
    >
    >
    > <?xml version="1.0" encoding="UTF-8"?>
    ><file name="file 2"/>
    >


    The results are correct at the time of printing.
    One element is allowed at root, and no content at root.
    Thats why its called 'ROOT' level.

    I just tried this for the first time (ie: dom tree), but
    it looks like its not condusive to creating a dom from scratch,
    more like its good for editing a tree.

    From below, it appears you have to be comfortable with objects,
    and creating a few helper subs might help a lot. The docs seem
    very thin (I just looked at it enough to do the below stuff).

    Good luck!
    -sln

    ------------------
    use strict;
    use warnings;

    use XML::LibXML;

    # Create document and ROOT
    my $doc = XML::LibXML::Document->new( '1.0', 'UTF-8' );
    my $root = $doc->createElement('root');
    $doc->setDocumentElement( $root );

    my $file = $doc->createElement('file');
    my $file2 = $doc->createElement('file2');

    # Add a 'file' child node under ROOT
    my $firstchild = $root->addChild( $file );

    # Add a 'name=' attribute to the 'file' clone
    $firstchild->setAttributeNode( $doc->createAttribute( "name", "file 1" ) );

    # Clone 'file' node, add a 'location=' attribute
    my $fileclone = $root->addChild( $file->cloneNode() );
    $fileclone->setAttributeNode( $doc->createAttribute( "location", "lost" ) );

    # Add a 'file2' child node under ROOT, with a 'name=' attribute
    $root->addChild( $doc->createElement('file2') )->
    setAttributeNode( $doc->createAttribute( "name", "file 2" ) );

    # Add a child node under the first 'file' node under ROOT
    $firstchild->addChild( $doc->createElement('child') );

    print "\n\n ";
    print $doc->toString(2);
    print "\n\n ";

    __END__

    <?xml version="1.0" encoding="UTF-8"?>
    <root>
    <file name="file 1">
    <child/>
    </file>
    <file name="file 1" location="lost"/>
    <file2 name="file 2"/>
    </root>
     
    , Feb 23, 2010
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Matt
    Replies:
    3
    Views:
    522
    Tor Iver Wilhelmsen
    Sep 17, 2004
  2. Ian Gregory
    Replies:
    1
    Views:
    510
  3. Iain
    Replies:
    2
    Views:
    679
  4. Olav
    Replies:
    3
    Views:
    4,259
  5. Ujwal
    Replies:
    0
    Views:
    115
    Ujwal
    Dec 4, 2003
Loading...

Share This Page