Connecting Tech Pros Worldwide Help | Site Map

Using XML DOM and XPATH together in perl

  #1  
Old June 12th, 2009, 03:27 PM
Member
 
Join Date: Dec 2007
Location: Pune, India
Posts: 46
Hello Perl Experts.
I am new to perl and seek help with some XML stuff.
My XML document is nested a few levels deep...so 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>
  40.  
  41.  
Thanks in advance,
Parag
  #2  
Old June 12th, 2009, 10:04 PM
Markus's Avatar
Moderator
 
Join Date: Jun 2007
Location: York, England, with wolves.
Posts: 4,861
Provided Answers: 9

re: Using XML DOM and XPATH together in perl


Would XML::XPath::setNodeText not work for you?

Mark.
  #3  
Old June 13th, 2009, 05:04 AM
Member
 
Join Date: Dec 2007
Location: Pune, India
Posts: 46

re: Using XML DOM and XPATH together in perl


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.

-Parag
  #4  
Old June 13th, 2009, 02:30 PM
Markus's Avatar
Moderator
 
Join Date: Jun 2007
Location: York, England, with wolves.
Posts: 4,861
Provided Answers: 9

re: Using XML DOM and XPATH together in perl


Quote:
Originally Posted by paragpdoke View Post

Till then...have a nice weekend.

-Parag
You too.

Mark.
  #5  
Old June 16th, 2009, 05:23 PM
Member
 
Join Date: Dec 2007
Location: Pune, India
Posts: 46

re: Using XML DOM and XPATH together in perl


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");
  4.  
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.

Thanks,
Parag
Reply

Tags
xml perl xpath dom