Hello, I am coding the script to handle my xml file. The step is to create a new node, search nodes in xml file with keyword, and replace it. I use XML::DOM module but I'm confused because the outcome is incorrect. Please help. Thank you. My XML file is:
<?xml version="1.0" encoding="UTF-8"?> <company name="ABC" branch="bangkok"> <employee code="emp0001"> <info> <general> <name>Somchai Chaisom</name> <photo>/employee/photo/emp001.gif</photo> <position>Helpdesk Officer</position> <department>IT</department> </general> </info> <education> <record> <institute>Mahidol</institute> <year>2548-2550</year> <degree>Master</degree> <faculty>IT</faculty> <major>IT</major> <gpa>2.6</gpa> </record> <record> <institute>Thammasat</institute> <year>2541-2545</year> <degree>Bachelor</degree> <faculty>Science</faculty> <major>Computer Science</major> <gpa>2.8</gpa> </record> </education> </employee> <employee code="emp0002"> <info> <general> <name>Manee Meena</name> <photo>/employee/photo/emp002.gif</photo> <position>Programmer</position> <department>IT</department> </general> </info> <education> <record> <institute>Siam University</institute> <year>2538-2542</year> <degree>Bachelor</degree> <faculty>Engineer</faculty> <major>Software Engineering</major> <gpa>3.3</gpa> </record> <record> <institute>Thammasat</institute> <year>2541-2545</year> <degree>Bachelor</degree> <faculty>Science</faculty> <major>Computer Science</major> <gpa>2.8</gpa> </record> </education> </employee> </company>
my PERL script is:
#!/usr/bin/perl use warnings; use XML::DOM; $link = "Thammasat"; %rlinks = ( "institute" => "test", "year" => "test", "degree" => "test", "faculty" => "test", "major" => "test", "gpa" => "test", ); my $rfile = "employee.xml"; my $xparser = XML::DOM::Parser->new(); my $xdoc = $xparser->parsefile($rfile); $ritem = $xdoc->createElement("record"); foreach $key (keys %rlinks) { $new = $xdoc->createElement("$key"); $text = $xdoc->createTextNode("$rlinks{$key}"); $new->appendChild($text); $ritem->appendChild($new); } foreach my $xitem ($xdoc->getElementsByTagName('record')) { $durl = $xitem->getElementsByTagName('institute')->item(0)->getFir +stChild->getNodeValue; if($durl eq $link) { $xdoc->getDocumentElement()->replaceChild($ritem,$xitem); } } $xdoc->printToFile("$rfile");

In reply to XML::DOM - NOT_FOUND_ERR by sureerat

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.