By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
437,809 Members | 1,997 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 437,809 IT Pros & Developers. It's quick & easy.

Using XML DOM and XPATH together in perl

P: 62
Hello Perl Experts.
I am new to perl and seek help with some XML stuff.
My XML document is nested a few levels I want to use xpath to find the exact element to work with. Then I wish to:
- Modify attribute values
- Add child elements
- Modify text inside existing child elements

I installed ActivePerl v5.10.0 built for MSWin32-x86-multi-thread and used ppm to install XML-DOM and XML-XPATH. However, I don't get how to use them together.

XML::XPATH will allow me to locate the ("mandatory" - refer XML below) element I need ... but probably need XML::DOM to modify the attribute or the elements.

How do I use XPATH and DOM together to perform the mentioned actions ? Any newbie example code snippets (or links to existing posts) will be greatly appreciated.

Here is an XML file similar to what I need:
Expand|Select|Wrap|Line Numbers
  1. <?xml version="1.0"?>
  2. <project>
  3.     <task>
  4.         <mode name="default">
  5.             <prerequisites>
  6.                 <actions>
  7.                     <action name="a1"/>
  8.                     <action name="a2"/>
  9.                 </actions>
  10.             </prerequisites>
  11.             <inputs>
  12.                 <item type="applications">App1</item>
  13.                 <item type="applications">App2</item>
  14.                 <item type="applications">App3</item>
  15.                 <item type="document">D1</item>
  16.                 <item type="file">F1</item>
  17.                 <item type="link">L1</item>
  18.             </inputs>
  19.             <action name="a1">
  20.                 <step name="S1">Detailed instructions vary for each step</step>
  21.                 <step name="S2">Detailed instructions vary for each step</step>
  22.                 <step name="S3">Detailed instructions vary for each step</step>
  23.                 <step name="S4">Detailed instructions vary for each step</step>
  24.                 <step name="S5">Detailed instructions vary for each step</step>
  25.                 <step name="S5.5" mandatory="true">Detailed instructions vary for each step</step>
  26.                 <step name="S6">Detailed instructions vary for each step</step>
  27.             </action>
  28.             <action name="a2">
  29.                 ...
  30.             </action>
  31.         </mode>
  32.         <mode name="minimal">
  33.             ...
  34.         </mode>
  35.     </task>
  36.     <task>
  37.         ...
  38.     </task>
  39. </project>
Thanks in advance,
Jun 12 '09 #1
Share this Question
Share on Google+
4 Replies

Expert 5K+
P: 6,050
Would XML::XPath::setNodeText not work for you?

Jun 12 '09 #2

P: 62
Hello Markus.
Thank you for your reply. I will try this on Monday (when I am back in office) and share my observations (and I am 99% sure, your suggestion will work).

Till then...have a nice weekend.

Jun 13 '09 #3

Expert 5K+
P: 6,050
You too.

Jun 13 '09 #4

P: 62
Hello Markus.
I tried this yesterday...but got stuck with some other problems.
Actually, I have worked on XML using VB 6.0 ... the API used there had the ability to load, query and save ... all as methods of the same object. I seem to be looking for something similar in Perl ... but cannot figure out how to do it.

If I am on the right track, I should be trying something on the lines of:
Expand|Select|Wrap|Line Numbers
  1. use XML::DOM::XPATH;
  2. my $xp = XML::XPATH->new(filename => "theFile.xml");
  3. $xp->setNodeText("/project/task/mode[@name="default"]/action/step[@mandatory="true"]" ,"false");
Next, I should load the in memory XML DOM into a DOM object and save it to the file system. This is where I am stuck.

My attempts so far had the DOM loading the XML from the file system ... which obviously did not have the modified attribute ... as the changes haven't been saved yet.

Sorry for getting stuck with trivial issues ... it is just that I am a newbie trying my first attempts at perl.

Jun 16 '09 #5

Post your reply

Sign in to post your reply or Sign up for a free account.