Solved

find a string in a xml file

Posted on 2008-06-13
16
196 Views
Last Modified: 2010-03-05
I need to extract values from a xml file which would look like this
<E Name="E" RdfName="EnumA" Namespace="abc">
                  <Label Label="E"A/>
                  <EnumValue Name="S" RdfName="xyz" Value="1" Namespace="abc"> <Label Label="xyz"/>
</EnumValue>

I need to find E Name="E" stirng and than extract the value of Label Label= and Value=. After extracting the vaues I need ot insert into a table.
could someone help me with this
0
Comment
Question by:khanzada19
  • 7
  • 5
  • 4
16 Comments
 
LVL 28

Expert Comment

by:FishMonger
ID: 21781859
0
 

Author Comment

by:khanzada19
ID: 21782175
I am not using perl 5.10.0, so I can't use examples from link
0
 
LVL 39

Expert Comment

by:Adam314
ID: 21782269
The XML::Simple module, and examples on the link, will work in versions of perl prior to 5.10.0 - as long as it's not to old.  What version of perl are you using?  Did you install the XML::Simple module?
0
 

Author Comment

by:khanzada19
ID: 21782291
I am using  perl, v5.6.0
0
 
LVL 28

Expert Comment

by:FishMonger
ID: 21782562
v5.6.0 is a little old, but still within reason, so the current version of XML::Simple should work for you.  However, I'd recommend upgrading to 5.8.8 or 5.10
0
 

Author Comment

by:khanzada19
ID: 21795230
becuase of the restriction on upgrade I won't be able to use perl. Dose anyone how to do this oracle PL/SQL.
0
 
LVL 39

Expert Comment

by:Adam314
ID: 21795363
You will not need to upgrade your perl in order to use perl with the XML::Simple module.  Have you tried to install the XML::Simple module?  Did it install successfully?  Any errors?
0
 

Author Comment

by:khanzada19
ID: 21795700
I can't even install the XML::Simple module, I mean I can install in dev but in prod, so this potion is out. I need to do it with PL/SQL
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 39

Expert Comment

by:Adam314
ID: 21796079
This will get the values from your XML file.  It works on the example provided, but there could be XML files that will break it.

As for getting it into a database, what interface do you want to use?  The DBI module is the most common method.  Can you use this?
#if $str contains the above XML (or replace $str here with whatever variable contains the XML)

if($str =~ /E\s+Name="E".*?<Label\s+Label="(.*?)".*?Value="(.*?)"/s) {

	print "Label=$1\nValue=$2\n";

}

Open in new window

0
 

Author Comment

by:khanzada19
ID: 21796266
I am doing following, seems like it reading a line at time instead of whole string

open(IN,"foo.xml")
          || die HisTools::output("ERROR: Could not open file $datapoint\n\n");
while ($str = <IN>){
print"\n a = $str\n";
if($str =~ /E\s+Name="E".*?<Label\s+Label="(.*?)".*?Value="(.*?)"/s) {
      print "Label=$1\nValue=$2\n";
}

close("foo.xml");
}
0
 
LVL 28

Expert Comment

by:FishMonger
ID: 21796514
You have 2 '<Label Label='  but only 1 'Value=' within '<E Name='
Do you need to extract both Label Label= tags or only one of them?

0
 

Author Comment

by:khanzada19
ID: 21796615
I need both 'Label Label='
0
 
LVL 39

Expert Comment

by:Adam314
ID: 21796947
The code I gave before will get only the first label.  Also, you need to read the entire file into memory, not one line at a time.  Try this
open(IN,"foo.xml")

  || die HisTools::output("ERROR: Could not open file $datapoint\n\n");

local $/;

my $str = <IN>;

close("foo.xml");
 

if($str =~ /<E\s+Name="E".*?<Label\s+Label="(.*?)".*?Value="(.*?)".*?<Label\s+Label="(.*?)"/s) {

	print "Label1=$1\nValue=$2\nLabel2=$3\n";

}

Open in new window

0
 

Author Comment

by:khanzada19
ID: 21797555
we are almost there, just one last thing how to handle if there are multiple entires

Thanks for your help!
0
 
LVL 39

Accepted Solution

by:
Adam314 earned 500 total points
ID: 21797632

while($str =~ /<E\s+Name="E".*?<Label\s+Label="(.*?)".*?Value="(.*?)".*?<Label\s+Label="(.*?)"/sg) {

	print "Label1=$1\nValue=$2\nLabel2=$3\n";

}

Open in new window

0
 
LVL 28

Expert Comment

by:FishMonger
ID: 21797654
Instead of loading the entire file into a scalar, you could loop over it line by line using the flip-flop operator.
while(<DATA>) {

   if ( /<E Name="E"/../<\/E>/ ) {

      if (/<Label Label="(.+?)"/) {

         print "Label=$1\n";

      }

      if (/Value="(.+?)"/) {

         print "Value=$1\n";

      }

   }

}
 

__DATA__

<E Name="E" RdfName="EnumA" Namespace="abc">

                  <Label Label="E"A/>

                  <EnumValue Name="S" RdfName="xyz" Value="1" Namespace="abc"> <Label Label="xyz"/></EnumValue>

                  <Label Label="F"A/>

                  <EnumValue Name="S" RdfName="xyz" Value="2" Namespace="abc"> <Label Label="zyx"/></EnumValue>

</E>

Open in new window

0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Email validation in proper way is  very important validation required in any web pages. This code is self explainable except that Regular Expression which I used for pattern matching. I originally published as a thread on my website : http://www…
I have been pestered over the years to produce and distribute regular data extracts, and often the request have explicitly requested the data be emailed as an Excel attachement; specifically Excel, as it appears: CSV files confuse (no Red or Green h…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

746 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now