Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

text to xml conversion

by nm_pavan (Initiate)
on May 24, 2011 at 04:51 UTC ( [id://906416]=perlquestion: print w/replies, xml ) Need Help??

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

param :PREFIX|numeric|sTAP|null param :CLK_PERIOD|numeric|10000|null param :STAP_NUMBER_OF_BITS_FOR_SLICE||16|null param :STAP_SIZE_OF_EACH_INSTRUCTION|numeric|8|8-100 param :STAP_ENABLE_VERCODE|boolean|0

I have a text file in this format how can I convert this to xml format & use xml parser to create a temp hash out of it

Please can anybody help in getting this code i am not familar with xml

I need that XML in some other part of the algo ... I need in the following format <Param> <numeric>numeric</numeric> <sTap>stap</sTap> <null>null</null> </param> In this format parent is the is part n other are its child

Replies are listed 'Best First'.
Re: text to xml conversion
by CountZero (Bishop) on May 24, 2011 at 06:48 UTC
    There is an infinite number of ways you can transfer these data into XML. It will all depend on the format of the XML-file and for that you need a DTD-file (Document_Type_Definition-file).

    But why should you need to go through an XML file to put your data into a hash?

    If you tell us how the hash should look like, it is quite certain someone will find a way to do it directly.

    CountZero

    A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James

      for that you need a DTD-file

      It's way better to use XMLSchema. In fact it seems the OP wants to model datatypes and how do you want to do that with a DTD? Unless you want to go down the DT4DTD road. DTDs are considered legacy nowadays. Of course it's all academic because as you indicate yourself there is no case for using XML:)

      Cheers

      Harry

        DTDs are considered legacy nowadays

        Hehe, I am legacy myself ...

        CountZero

        A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James

Re: text to xml conversion
by davido (Cardinal) on May 24, 2011 at 06:51 UTC

    Most people wouldn't drive from their home in Los Angeles, to New York, and back to Los Angeles to visit their next door neighbor. If you want to convert that data to a hash, why do you have to take a trip to XML along the way? And what do you want the XML to look like? What is the specification (DTD)?

    For that matter, what do you want the hash to look like? I see an array (param :, param :, param :) of hashes of arrays. But maybe I should squint a little harder.

    Update: lol, I think CountZero and I must have been drinking the same Kool-Aid.


    Dave

Re: text to xml conversion
by roboticus (Chancellor) on May 24, 2011 at 10:17 UTC

    nm_pavan:

    You should be able to turn that into an XML document something like this:

    use strict; use warnings; my $XML = ""; while (<DATA>) { chomp; $XML .= "<line$.>$_</line$.>"; } print join("", '<?xml version="1.0">', '<document>', $XML, '</document>' ); __DATA__ param :PREFIX|numeric|sTAP|null param :CLK_PERIOD|numeric|10000|null param :STAP_NUMBER_OF_BITS_FOR_SLICE||16|null param :STAP_SIZE_OF_EACH_INSTRUCTION|numeric|8|8-100 param :STAP_ENABLE_VERCODE|boolean|0

    Note: I'm assuming your data doesn't include characters that need encoding. If it does, be sure to visit CPAN and get the appropriate encoding modules and encode the data before including it in your XML string.

    Parsing XML into a hash has been beaten to death, so you can use search to find appropriate nodes. I could do it for you, but as others have mentioned, it's a bit far to go since you just want a hash. To convert to a hash directly would be simpler:

    use strict; use warnings; use Data::Dumper; my %Hash = (); while (<DATA>) { chomp; $Hash{line$.} = $_; } print Dumper(\%Hash); __DATA__ param :PREFIX|numeric|sTAP|null param :CLK_PERIOD|numeric|10000|null param :STAP_NUMBER_OF_BITS_FOR_SLICE||16|null param :STAP_SIZE_OF_EACH_INSTRUCTION|numeric|8|8-100 param :STAP_ENABLE_VERCODE|boolean|0

    Pretty easy, yes?

    Of course, it's utterly useless, as I'm sure neither the XML document nor the hash are structured the way you want. But as you don't specify what you're looking for, and show no particular interest in doing it (as evidenced by the amount of effort shown), it at least fills the stated requirements...

    Of course, the code is untested, so if there are any errors, you get to fix 'em.

    </snarky-mode>

    ...roboticus

    When your only tool is a hammer, all problems look like your thumb.

Re: text to xml conversion
by Anonymous Monk on May 24, 2011 at 06:42 UTC

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://906416]
Approved by sundialsvc4
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others sharing their wisdom with the Monastery: (4)
As of 2024-03-29 10:46 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found