Re: How can I add missing XML open tags
by Crackers2 (Parson) on Dec 03, 2013 at 18:41 UTC
|
| [reply] |
|
| [reply] |
|
| [reply] |
Re: How can I add missing XML open tags
by toolic (Bishop) on Dec 03, 2013 at 18:40 UTC
|
XML::Twig empty_tags => 'expand' looks like it does what you want. I shortened your XML and fixed the closing "orders tag":
use warnings;
use strict;
use XML::Twig;
my $xml = <<XML;
<orders>
<order>
<alt_number>2222</alt_number>
<state>processed</state>
<user_id>1234</user_id>
<dd_id>54321</dd_id>
<placed_at/>
<total_price>449.0</total_price>
<total_shipping_costs>90.0</total_shipping_costs>
<discount>0</discount>
<dd_status_code>HD</dd_status_code>
<dd_status_desc>ORDER IS ON HOLD UNTIL 11/01/13</dd_status_desc>
<promo_code/>
</order>
</orders>
XML
my $twig = XML::Twig->new(
pretty_print => 'indented',
empty_tags => 'expand',
);
$twig->parse($xml);
$twig->print;
__END__
Output:
<orders>
<order>
<alt_number>2222</alt_number>
<state>processed</state>
<user_id>1234</user_id>
<dd_id>54321</dd_id>
<placed_at></placed_at>
<total_price>449.0</total_price>
<total_shipping_costs>90.0</total_shipping_costs>
<discount>0</discount>
<dd_status_code>HD</dd_status_code>
<dd_status_desc>ORDER IS ON HOLD UNTIL 11/01/13</dd_status_desc>
<promo_code></promo_code>
</order>
</orders>
| [reply] [d/l] |
|
Can XML::Twig be used to create a simple tab delimited CSV file?
| [reply] |
Re: How can I add missing XML open tags
by hippo (Bishop) on Dec 03, 2013 at 18:31 UTC
|
I suggest trying a canonicalizer such as XML::CanonicalizeXML. Your supplied data is not valid XML to begin with, so you would need to address that problem first, I suspect.
| [reply] |
|
The only problem with it is the closing tag <orders>, which is missing the all-important slash: </orders>. Other than this single typo, the XML is well-formed. (Whether or not the XML is "valid" is a different matter altogether.)
| [reply] [d/l] [select] |
|
The trailing <orders> was a typo it is </orders>
| [reply] |
Re: How can I add missing XML open tags
by Jim (Curate) on Dec 03, 2013 at 22:54 UTC
|
Personally, I'd just do this: s{<([^/>]+?)/>}{<$1></$1>}g. Only if this didn't work for some peculiar reason would I bother with a more complicated solution.
Jim
UPDATE: A possibly better alternative: s{<(\w+)/>}{<$1></$1>}ag. (Notice the /a character set modifier, which is purposeful.)
| [reply] [d/l] [select] |
|
That was surprisingly very effective.
Thanks!
| [reply] |
|
| [reply] [d/l] [select] |
Re: How can I add missing XML open tags
by Laurent_R (Canon) on Dec 03, 2013 at 18:44 UTC
|
I don't know of there is any module to do that, but if you want to do it manually, you could have a hash in which the keys will be the tag name (probably stripped of the < and >). Parse your file, for each opening tag, increment the value of the hash for that tag. For each closing tag, strip the </ and >, see if the hash exists for that tag and if the value is 0: if the hash does not exist for that tag or the value is zero, add an opening tag; if the value exists and if the value is larger than 0, decrement the hash value for that tag.
EDIT: well it seems there is a module, I had not seen the previous answers when I started to post mine. Forget the above and use the module.
| [reply] |