| 1 | use XML::SimpleObject::LibXML; | ||
| 2 | use strict; | ||
| 3 | |||
| 4 | my $XML = <<EOF; | ||
| 5 | |||
| 6 | <files> | ||
| 7 | <file type="symlink"> | ||
| 8 | <name>/etc/dosemu.conf</name> | ||
| 9 | <dest>dosemu.conf-drdos703.eval</dest> | ||
| 10 | <bytes><hi>cool</hi>20</bytes> | ||
| 11 | </file> | ||
| 12 | <file> | ||
| 13 | <name>/etc/passwd</name> | ||
| 14 | <bytes>948</bytes> | ||
| 15 | </file> | ||
| 16 | </files> | ||
| 17 | |||
| 18 | EOF | ||
| 19 | |||
| 20 | my $xmlobj = new XML::SimpleObject::LibXML(XML => $XML); | ||
| 21 | |||
| 22 | # or: | ||
| 23 | # my $parser = new XML::LibXML; | ||
| 24 | # my $xmlobj = new XML::SimpleObject::LibXML ($parser->parse_string($XML)); | ||
| 25 | |||
| 26 | print "\n"; | ||
| 27 | |||
| 28 | { | ||
| 29 | print "Files: \n"; | ||
| 30 | foreach my $element ($xmlobj->child("files")->children("file")) | ||
| 31 | { | ||
| 32 | print " filename: " . $element->child("name")->value . "\n"; | ||
| 33 | if ($element->attribute("type")) | ||
| 34 | { | ||
| 35 | print " type: " . $element->attribute("type") . "\n"; | ||
| 36 | } | ||
| 37 | print " bytes: " . $element->child("bytes")->value . "\n"; | ||
| 38 | } | ||
| 39 | } | ||
| 40 | |||
| 41 | print "\n"; | ||
| 42 | |||
| 43 | { | ||
| 44 | my $filesobj = $xmlobj->child("files")->child("file"); | ||
| 45 | foreach my $child ($filesobj->children) { | ||
| 46 | print "child: ", $child->name, ": ", $child->value, "\n"; | ||
| 47 | } | ||
| 48 | } | ||
| 49 | |||
| 50 | print "\n"; | ||
| 51 | |||
| 52 | { | ||
| 53 | my $filesobj = $xmlobj->child("files"); | ||
| 54 | foreach my $childname ($filesobj->children_names) { | ||
| 55 | print "$childname has children: "; | ||
| 56 | print join (", ", $filesobj->child($childname)->children_names), "\n"; | ||
| 57 | } | ||
| 58 | } | ||
| 59 | |||
| 60 | print "\n"; | ||
| 61 | |||
| 62 | { | ||
| 63 | print "First filename: " . $xmlobj->xpath_search("/files/file[1]/name")->value . "\n"; | ||
| 64 | } | ||
| 65 | |||
| 66 | { | ||
| 67 | $xmlobj->xpath_search("/files/file[1]/name")->value("testfilename.txt"); | ||
| 68 | print "Changed first filename: " . $xmlobj->xpath_search("/files/file[1]/name")->value . "\n"; | ||
| 69 | } | ||
| 70 | |||
| 71 | { | ||
| 72 | my $filenode = $xmlobj->xpath_search("/files/file[1]"); | ||
| 73 | $filenode->attribute("type","newtype"); | ||
| 74 | print "Changed attribute value: " . $filenode->attribute("type") . "\n"; | ||
| 75 | } | ||
| 76 | |||
| 77 | { | ||
| 78 | $xmlobj->xpath_search("/files/file[1]")->add_child("owner" => "Dan", attr => { phone => "123-2345" } ); | ||
| 79 | print "Added child: " . $xmlobj->xpath_search("/files/file[1]/owner")->value . "\n"; | ||
| 80 | } | ||
| 81 | |||
| 82 | { | ||
| 83 | $xmlobj->xpath_search("/files/file[2]")->add_attribute("new_attr" => "new_value"); | ||
| 84 | print "Added attribute: " . $xmlobj->xpath_search("/files/file[2]")->attribute("new_attr") . "\n"; | ||
| 85 | } | ||
| 86 | |||
| 87 | { | ||
| 88 | $xmlobj->xpath_search("/files")->name("Files"); | ||
| 89 | print "Changed element name: " . $xmlobj->child()->name . "\n"; | ||
| 90 | } | ||
| 91 | |||
| 92 | { | ||
| 93 | $xmlobj->xpath_search("/Files/file[1]/dest")->delete; | ||
| 94 | $xmlobj->xpath_search("/Files/file[1]")->value(""); | ||
| 95 | print "Deleted element.\n"; | ||
| 96 | } | ||
| 97 | # $xmlobj->replace_names_values(xpath => "/Files/file/name", value => "allyourfilesowned", name => "myname"); | ||
| 98 | # $xmlobj->delete_nodes(xpath => "/Files/file"); | ||
| 99 | { | ||
| 100 | print "Output: " . $xmlobj->output_xml; | ||
| 101 | } |