Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

Re: XML::Simple XML / XMLin / XMLout? or something else?

by BrowserUk (Patriarch)
on Oct 12, 2012 at 14:02 UTC ( [id://998685]=note: print w/replies, xml ) Need Help??


in reply to XML::Simple XML / XMLin / XMLout? or something else?

With your posted example after the __DATA_ tag, this:

#! perl -slw use strict; use XML::Simple; my $xml = XMLin( \*DATA ); print XMLout( $_, NoAttr => 1, RootName => 'book' ) for @{ $xml->{book +} }; __DATA__

Produces this:

C:\test>junk <book> <author>Book 1 author 1</author> <author>Book 1 author 2</author> <isbn>Book1ISBN</isbn> <title>Book 1 title</title> </book> <book> <author>Book 2 author 1</author> <author>Book 2 author 2</author> <isbn>Book2ISBN</isbn> <title>Book 2 title</title> </book> <book> <author>Book 3 author 1</author> <author>Book 3 author 2</author> <author>Book 3 author 3</author> <isbn>Book3ISBN</isbn> <title>Book 3 title</title> </book>

With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.

RIP Neil Armstrong

Replies are listed 'Best First'.
Re^2: XML::Simple XML / XMLin / XMLout? or something else?
by Roboz (Novice) on Oct 12, 2012 at 14:26 UTC

    This also looks like it'll do what I need. Man all you folk are awesome! Thanks for the help.

      I can't resist: How does the output of the last approach differ from mine where I said you have to just throw away the <booklist> tag from your input file? By the way: Extra newlines are also simple.

      But if you really need perl:

      perl -ne 'print unless /booklist/' < data.xml

      Best regards
      McA

        How about if the xml gets received like this?

        <booklist> <book> <author>Book 1 author 1</author> <author>Book 1 auth +or 2</author> <title>Book 1 title</title> <isbn>Book1ISBN</isbn> </bo +ok> <book> <author>Book 2 author 1</author> <author>Book 2 author 2</ +author> <title>Book 2 title</title> <isbn>Book2ISBN</isbn> </book> <b +ook> <author>Book 3 author 1</author> <author>Book 3 author 2</author +> <author>Book 3 author 3</author> <title>Book 3 title</title> <isbn> +Book3ISBN</isbn> </book> </booklist>

        With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
        Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
        "Science is about questioning the status quo. Questioning authority".
        In the absence of evidence, opinion is indistinguishable from prejudice.

        RIP Neil Armstrong

        Hey McA,

        One possibility (that I've run into myself) is $corporate mandates that say that data can't be edited by hand, and that you have to write a program (to be executed only by those with sufficient authority) to modify said data.

        Of course, another possibility is that this is more than a one-time thing -- maybe these book list files show up daily.

        A warning about your one-liner (cool though it is), XML documents don't necessarily have to have newlines between elements. I've encountered some with no newlines at all -- I believe your solution would empty the file in that case.

        Just some random thoughts...

        Update: And BrowserUk phrases it better than I :)

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others browsing the Monastery: (8)
As of 2024-03-28 11:12 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found