in reply to XML Attribute vs Element
Please do not link off site code. Include it in your node (in readmore tags if it is large). Off site links are prone to breakage and PerlMonks nodes are read by people for years after their initial posting. So, if at all possible keep your node self contained.
Also note that </br> on its own is not valid HTML or XHTML. You can use <br /> for XHTML, but this is more an HTML site so use <br>, or better still, don't use break tags at all!
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: XML Attribute vs Element
by Sporti69 (Acolyte) on Nov 06, 2008 at 20:22 UTC | |
thank you all for helping me. Not only in this node. XML But I could still use some help... When I add an attribute to lets say tag B my code fails all of a sudden ... Can someone tell me why / advise or point out a solution... The error I get is "Can't use string ("value of attribute") as an ARRAY ref while "strict refs"" Thanks so much ! | [reply] [d/l] [select] |
by ig (Vicar) on Nov 06, 2008 at 22:09 UTC | |
When I add an attribute to lets say tag B... You are getting the error message because XML::Simple creates a data structure with an array for your elements but a simple scalar value for your attribute. You can see this if you use Data::Dumper to dump $doc after loading your file. After changing your B tag to <B type="xxx"> your script produced the following error: Can't use string ("xxx") as an ARRAY ref while "strict refs" in use. Here is the structure produced by XMLin: Read more... (2 kB)
If you look carefully at what has been created for your tag B, you will see that there are three elements in the contained hash, with keys D, C and type. The values of the first two (D and C) are references to arrays but the value of the third is a string. Your program uses all three as if they were references to arrays. Because you have used use strict; (which is a good thing) perl stops when it gets to the string and reports the error. You can either write your program to check whether a value is a reference or not, using ref or you can provide parameters to XML::Simple to make it load the elements and tags consistently. Which approach is better will depend on what you are doing. You may want to learn both methods so you can decide which is most appropriate. | [reply] [d/l] |
by Sporti69 (Acolyte) on Nov 07, 2008 at 10:19 UTC | |
You can either write your program to check whether a value is a reference or not, using ref or you can provide parameters to XML::Simple to make it load the elements and tags consistently. Which approach is better will depend on what you are doing. You may want to learn both methods so you can decide which is most appropriate. I will create a check whether it is a reference or not. Will look into this right now and will keep you (guys) posted. Providing parameters to XML::Simple is not an option because I want to create a script that runs universal. Both on element depth and on attributes. So the reusability will be high. @ grandfather: thank you for the suggestion, but you use indent size = 4. This can be variable. I will write the complete script using XML::Simple cause it feels so simple :) (after a lot of coarsing by this perl newbie). This will also mean that the for loops for $Sub1 2, 3, 4 will need to be put in a loop aswell. Probably together with the 'ref' method Keeping you guys posted SportiPS: For whom is interested, the excel part will be multiple worksheets with data from various tags and elements in a table style | [reply] |
by GrandFather (Saint) on Nov 06, 2008 at 22:16 UTC | |
I'd use something simpler for parsing the XML than XML::Simple, like XML::Parser, although I don't know what you want the final output for so the rendering below may not suit you:
Prints:
Perl reduces RSI - it saves typing | [reply] [d/l] [select] |
by Sporti69 (Acolyte) on Nov 07, 2008 at 12:14 UTC | |
| [reply] |
by ig (Vicar) on Nov 06, 2008 at 22:29 UTC | |
You should probably change
to
Update: no, your other counts are fine. And you don't need the scalar as you are assigning to a scalar, so you can do as you have done for the other counts:
| [reply] [d/l] [select] |
by ikegami (Patriarch) on Nov 06, 2008 at 21:09 UTC | |
What code do you use to do that? I used
| [reply] [d/l] |