devlele has asked for the wisdom of the Perl Monks concerning the following question:

Hi,

Following is the xml file which I have. Now in this file, I need all the values between fields <FormattedReportObjects>xyz</FormattedReportObjects>, thus I need all such "xyz" values and want to store in an array. Can anyone help me to do that ?

Thanks.

<?xml version="1.0" encoding="UTF-8" ?> - <FormattedReport xmlns="urn:crystal-reports:schemas" xmlns:xsi="http +://www.w3.org/2000/10/XMLSchema-instance"> - <FormattedAreaPair Level="0" Type="Report"> - <FormattedArea Type="Header"> - <FormattedSections> - <FormattedSection SectionNumber="0"> - <FormattedReportObjects> - <FormattedReportObject xsi:type="CTFormattedSubreport"> <ObjectName>Subreport1</ObjectName> - <FormattedAreaPair Level="0" Type="Report"> - <FormattedArea Type="Header"> - <FormattedSections> - <FormattedSection SectionNumber="0"> <FormattedReportObjects /> </FormattedSection> - <FormattedSection SectionNumber="1"> <FormattedReportObjects /> </FormattedSection> </FormattedSections> </FormattedArea> - <FormattedAreaPair Level="1" Type="Details"> - <FormattedArea Type="Details"> - <FormattedSections> - <FormattedSection SectionNumber="0"> <FormattedReportObjects /> </FormattedSection> </FormattedSections> </FormattedArea> </FormattedAreaPair> - <FormattedArea Type="Footer"> - <FormattedSections> - <FormattedSection SectionNumber="0"> <FormattedReportObjects /> </FormattedSection> - <FormattedSection SectionNumber="1"> <FormattedReportObjects /> </FormattedSection> </FormattedSections> </FormattedArea> </FormattedAreaPair> </FormattedReportObject> </FormattedReportObjects> </FormattedSection> - <FormattedSection SectionNumber="1"> - <FormattedReportObjects> - <FormattedReportObject xsi:type="CTFormattedSubreport"> <ObjectName>Subreport2</ObjectName> - <FormattedAreaPair Level="0" Type="Report"> - <FormattedArea Type="Header"> <FormattedSections /> </FormattedArea> - <FormattedArea Type="Footer"> <FormattedSections /> </FormattedArea> </FormattedAreaPair> </FormattedReportObject> </FormattedReportObjects> </FormattedSection> </FormattedSections> </FormattedArea> - <FormattedAreaPair Level="1" Type="Group"> - <FormattedArea Type="Header"> - <FormattedSections> - <FormattedSection SectionNumber="0"> - <FormattedReportObjects> - <FormattedReportObject xsi:type="CTFormattedField" Type="xsd:string" + FieldName="GroupName ({Data.SYSTEM})"> <ObjectName>Field4</ObjectName> <FormattedValue>System lgloe040</FormattedValue> <Value>System lgloe040</Value> </FormattedReportObject> </FormattedReportObjects> </FormattedSection> </FormattedSections> </FormattedArea> - <FormattedAreaPair Level="2" Type="Group"> - <FormattedArea Type="Header"> - <FormattedSections> - <FormattedSection SectionNumber="0"> - <FormattedReportObjects> - <FormattedReportObject xsi:type="CTFormattedField" Type="xsd:string" + FieldName="GroupName ({Data.TYPE})"> <ObjectName>Field5</ObjectName> <FormattedValue>Type LOG</FormattedValue> <Value>Type LOG</Value> </FormattedReportObject> </FormattedReportObjects> </FormattedSection> </FormattedSections> </FormattedArea> - <FormattedAreaPair Level="3" Type="Details"> - <FormattedArea Type="Details"> - <FormattedSections> - <FormattedSection SectionNumber="0"> - <FormattedReportObjects> - <FormattedReportObject xsi:type="CTFormattedField" Type="xsd:string" + FieldName="{Data.OWNER}"> <ObjectName>Field6</ObjectName> <FormattedValue>\</FormattedValue> <Value>\</Value> </FormattedReportObject> - <FormattedReportObject xsi:type="CTFormattedField" Type="xsd:string" + FieldName="{Data.PATH}"> <ObjectName>Field10</ObjectName> <FormattedValue>C:\abc\</FormattedValue> <Value>C:\abc\</Value> </FormattedReportObject> - <FormattedReportObject xsi:type="CTFormattedField" Type="xsd:string" + FieldName="{Data.NAMETYPE}"> <ObjectName>Field16</ObjectName> <FormattedValue>weru3453.log</FormattedValue> <Value>weru3453.log</Value> </FormattedReportObject> - <FormattedReportObject xsi:type="CTFormattedField" Type="xsd:string" + FieldName="{@DBS_FileSize}"> <ObjectName>Field15</ObjectName> <FormattedValue>4</FormattedValue> <Value>4</Value> </FormattedReportObject> - <FormattedReportObject xsi:type="CTFormattedField" Type="xsd:string" + FieldName="{@DBS_DateAcc}"> <ObjectName>Field8</ObjectName> <FormattedValue>12/12/2005</FormattedValue> <Value>12/12/2005</Value> </FormattedReportObject> - <FormattedReportObject xsi:type="CTFormattedField" Type="xsd:string" + FieldName="{@DBS_DateMod}"> <ObjectName>Field9</ObjectName> <FormattedValue>12/12/2005</FormattedValue> <Value>12/12/2005</Value> </FormattedReportObject> - <FormattedReportObject xsi:type="CTFormattedField" Type="xsd:string" + FieldName="{@DBS_DateCre}"> <ObjectName>Field11</ObjectName> <FormattedValue>12/12/2005</FormattedValue> <Value>12/12/2005</Value> </FormattedReportObject> </FormattedReportObjects> </FormattedSection> </FormattedSections> </FormattedArea> </FormattedAreaPair> - <FormattedAreaPair Level="3" Type="Details"> - <FormattedArea Type="Details"> - <FormattedSections> - <FormattedSection SectionNumber="0"> - <FormattedReportObjects> - <FormattedReportObject xsi:type="CTFormattedField" Type="xsd:string" + FieldName="{Data.OWNER}"> <ObjectName>Field6</ObjectName> <FormattedValue>\</FormattedValue> <Value>\</Value> </FormattedReportObject> - <FormattedReportObject xsi:type="CTFormattedField" Type="xsd:string" + FieldName="{Data.PATH}"> <ObjectName>Field10</ObjectName> <FormattedValue>C:\abc\</FormattedValue> <Value>C:\abc\</Value> </FormattedReportObject> - <FormattedReportObject xsi:type="CTFormattedField" Type="xsd:string" + FieldName="{Data.NAMETYPE}"> <ObjectName>Field16</ObjectName> <FormattedValue>sdfsdfs.log</FormattedValue> <Value>sdfsdfs.log</Value> </FormattedReportObject> - <FormattedReportObject xsi:type="CTFormattedField" Type="xsd:string" + FieldName="{@DBS_FileSize}"> <ObjectName>Field15</ObjectName> <FormattedValue>4</FormattedValue> <Value>4</Value> </FormattedReportObject> - <FormattedReportObject xsi:type="CTFormattedField" Type="xsd:string" + FieldName="{@DBS_DateAcc}"> <ObjectName>Field8</ObjectName> <FormattedValue>12/12/2005</FormattedValue> <Value>12/12/2005</Value> </FormattedReportObject> - <FormattedReportObject xsi:type="CTFormattedField" Type="xsd:string" + FieldName="{@DBS_DateMod}"> <ObjectName>Field9</ObjectName> <FormattedValue>12/12/2005</FormattedValue> <Value>12/12/2005</Value> </FormattedReportObject> - <FormattedReportObject xsi:type="CTFormattedField" Type="xsd:string" + FieldName="{@DBS_DateCre}"> <ObjectName>Field11</ObjectName> <FormattedValue>12/12/2005</FormattedValue> <Value>12/12/2005</Value> </FormattedReportObject> </FormattedReportObjects> </FormattedSection> </FormattedSections> </FormattedArea> </FormattedAreaPair> - <FormattedArea Type="Footer"> - <FormattedSections> - <FormattedSection SectionNumber="0"> - <FormattedReportObjects> - <FormattedReportObject xsi:type="CTFormattedField" Type="xsd:string" + FieldName="{@DBS_FileSizeSum_Typ}"> <ObjectName>Field24</ObjectName> <FormattedValue>8</FormattedValue> <Value>8</Value> </FormattedReportObject> </FormattedReportObjects> </FormattedSection> </FormattedSections> </FormattedArea> </FormattedAreaPair> - <FormattedArea Type="Footer"> - <FormattedSections> - <FormattedSection SectionNumber="0"> - <FormattedReportObjects> - <FormattedReportObject xsi:type="CTFormattedText"> <ObjectName>Text10</ObjectName> <TextValue>System Total</TextValue> </FormattedReportObject> - <FormattedReportObject xsi:type="CTFormattedField" Type="xsd:string" + FieldName="{@DBS_FileSizeSum_Sys}"> <ObjectName>Field17</ObjectName> <FormattedValue>8</FormattedValue> <Value>8</Value> </FormattedReportObject> </FormattedReportObjects> </FormattedSection> </FormattedSections> </FormattedArea> </FormattedAreaPair> - <FormattedArea Type="Footer"> - <FormattedSections> - <FormattedSection SectionNumber="0"> - <FormattedReportObjects> - <FormattedReportObject xsi:type="CTFormattedText"> <ObjectName>Text11</ObjectName> <TextValue>Report Total</TextValue> </FormattedReportObject> - <FormattedReportObject xsi:type="CTFormattedField" Type="xsd:string" + FieldName="{@DBS_FileSizeSum_Rpt}"> <ObjectName>Field25</ObjectName> <FormattedValue>8</FormattedValue> <Value>8</Value> </FormattedReportObject> </FormattedReportObjects> </FormattedSection> </FormattedSections> </FormattedArea> </FormattedAreaPair> </FormattedReport>

Formatting fixed by GrandFather

Replies are listed 'Best First'.
Re: parsing xml file and get the values
by GrandFather (Saint) on Dec 14, 2005 at 23:43 UTC

    Show us what you have tried. Reduce the sample XML to a few lines. Show us what you want to see. Read most of the nodes linked by the 'Welcome to the Monastery' section of Tutorials and I know what I mean. Why don't you?.


    DWIM is Perl's answer to Gödel
Re: parsing xml file and get the values
by GrandFather (Saint) on Dec 15, 2005 at 00:00 UTC

    This may get you started:

    use strict; use warnings; use XML::TreeBuilder; my $root = XML::TreeBuilder->new; my $xml = do {local $/; <DATA>}; $root->parse ($xml); my @elements = $root->find ('FormattedReportObjects'); print $_->as_text () . "\n" for @elements;

    Prints:

    Subreport1 Subreport2 Field4 System lgloe040 System lgloe040 Field5 Type LOG Type LOG Field6 \ \ Field10 C:\abc\ C:\abc\ Field16 weru3453.log weru3 +453.log Field15 4 4 Field8 12/12/2005 12/12/2005 Field9 12/1 +2/2005 12/12/2005 Field11 12/12/2005 12/12/2005 Field6 \ \ Field10 C:\abc\ C:\abc\ Field16 sdfsdfs.log sdfsdf +s.log Field15 4 4 Field8 12/12/2005 12/12/2005 Field9 12/12/ +2005 12/12/2005 Field11 12/12/2005 12/12/2005 Field24 8 8 Text10 System Total Field17 8 8 Text11 Report Total Field25 8 8

    Note that the original XML sample data seems to contain - characters between elements that give TreeBuilder grief. I've removed those.


    DWIM is Perl's answer to Gödel
      Thanks a lot GrandFather. But when I am running this code, I am getting following error, " Name "main::DATA" used only once: possible typo at xml_parser.pl line 8. readline() on unopened filehandle DATA at xml_parser.pl line 8. no element found at line 1, column 0, byte -1 at C:/Perl/site/lib/XML/Parser.pm line 187" Please help. Thanks.

        Click on the readmore link in my previous node and you will discover a __DATA__ section that contains your, slightly edited, original sample XML. Read I know what I mean. Why don't you? to understand why I used the __DATA__ section and read Writeup Formatting Tips to learn about formatting your node and things like code tags and readmore tags.


        DWIM is Perl's answer to Gödel
Re: parsing xml file and get the values
by davido (Cardinal) on Dec 15, 2005 at 02:56 UTC

    Use XML::Simple. Be sure to read about, and use the forceArray => 1 option. Then use Data::Dumper to dump the output from XML::Simple so you can see what part of the data structure you need to probe to uncover the specific values you're seeking. Once you discover that, the rest is trivial.


    Dave

Re: parsing xml file and get the values
by SamCG (Hermit) on Dec 15, 2005 at 00:10 UTC
    You might also try formatting your post. It's hard to see where your question ends and the xml begins. Try putting <c> tags around the xml.

    It's a small pain to use formatting tags, but if you're used to html/xml you should at least be accustomed to them. Note in the event you ever try linking, there are some special rules.

    Welcome to the monastery, I hope you have a warm and didactic stay.