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

Hi, I am fairly new to Perl and got a task where I need some help. I want to parse a xml file which has its own xml structure into an array and manipulate the data in the xml. Depending on other child information within the xml file I want to create another parent/child in it. I tried the package XML::SIMPLE and I can read out an XML but when I try to read my data by just selection the info I need it wont return a value. This is my dtd: (snipe)
<!ELEMENT DEFJOB (JOB+)> <!ELEMENT JOB (TAG_NAMES*,(TAG_NAMES | INCOND | OUTCOND | AUTOEDIT | Q +UANTITATIVE | CONTROL | SHOUT | STEP_RANGE | ON)*)> <!ELEMENT INCOND EMPTY> <!ELEMENT OUTCOND EMPTY> <!ELEMENT AUTOEDIT EMPTY> <!ELEMENT QUANTITATIVE EMPTY> <!ELEMENT CONTROL EMPTY> <!ELEMENT SHOUT EMPTY> <!ELEMENT STEP_RANGE EMPTY> <!ELEMENT ON (DO|DOAUTOEDIT|DOSHOUT|DOFORCEJOB|DOCTBRULE|DOSYSOUT|DOIF +RERUN|DOCOND|DOMAIL)*> <!ELEMENT DO EMPTY> <!ELEMENT DOSHOUT EMPTY> <!ELEMENT DOCOND EMPTY> <!ELEMENT DOAUTOEDIT EMPTY> <!ELEMENT DOFORCEJOB EMPTY> <!ELEMENT DOSYSOUT EMPTY> <!ELEMENT DOIFRERUN EMPTY> <!ELEMENT DOMAIL EMPTY> <!ELEMENT DOCTBRULE EMPTY> <!ELEMENT TAG_NAMES EMPTY> <!ATTLIST TAG_NAMES TAG_NAME CDATA #IMPLIED CDATA #IMPLIED>
Below is a xml example: Within the data-set I find the GROUP / NODEID from witch I need to create the new child "QUANTITATIVE NAME".
<?xml version='1.0' encoding='ISO-8859-1' ?> <!DOCTYPE DEFJOB SYSTEM "/aa/em/ddd/e62xstf3/resource/defjob.dtd"> <DEFJOB > <JOB GROUP="xp_maint" NODEID="localhost" USE_INSTREAM_JCL="0" > <QUANTITATIVE NAME="QR1-INIT" QUANT="1" /> <QUANTITATIVE NAME="QR2-NODEID" QUANT="1" /> <QUANTITATIVE NAME="QR3-GROUP" QUANT="1" /> /> </ON> </JOB>
My Question: 1. What would be the best package to use? XML::SIMPLE? I was thinking to read the data into an array, then to see if any "QUANTITATIVE NAME" is used, if not I would create new ones with the GROUP / NODEID. Thanks in advanced for any help

Replies are listed 'Best First'.
Re: Reading and Manipulation XML Files
by Anonymous Monk on Mar 26, 2010 at 11:02 UTC
    tried the package XML::SIMPLE and I can read out an XML but when I try to read my data by just selection the info I need it wont return a value.

    What? Also, your XML isn't valid.

    1. What would be the best package to use? XML::SIMPLE?

    It depends. You can't go wrong with XML::Twig, XML::Rules, XML::LibXML...

    I was thinking to read the data into an array, then to see if any "QUANTITATIVE NAME" is used, if not I would create new ones with the GROUP / NODEID

    Um, try it and you'll see why its a bad idea, but if you can make it work, or like suffering .... to each his own :)

      Thanks for your answer, much appreciated. I didn't want to spam the page so I didn't add the entire xml/dtd just a snipe of it. As it still feels somewhat hard to describe my problem technical and on the dot I tried not to over-spam. Now you did say that it is rather a bad idea, what would you suggest how I can solve this sleeker? The aim of this task is to prove a migration script that would read an extract (out.xml) and look for this QuantResources,and complete them depending on there NodeID/Group. Then the System would be able to control the components better. This is for a tool called ControlM. thanks again
        ..so I didn't add the entire xml/dtd just a snipe of it.

        Thats fine, but you could have made it a valid snip, unless the purpose of your post was to troubleshoot your xml (which would be off topic , How (Not) To Ask A Question)

        . Now you did say that it is rather a bad idea, what would you suggest how I can solve this sleeker?

        Like I already said and showed. Using proper parser always beats hand parsing xml.

Re: Reading and Manipulation XML Files
by Anonymous Monk on Mar 26, 2010 at 11:08 UTC