-1

The next convert test.xml to json:

perl -MJSON::Any -MXML::Simple -le'print JSON::Any->new()->objToJson(XMLin("/tmp/test.xml "))'

but I need convert any xml (example test-1.xml test-2.xml test-3.xml test-4.xml etc) with pattern name /tmp/test-*.xml, but if I use:

perl -MJSON::Any -MXML::Simple -le'print JSON::Any->new()->objToJson(XMLin("/tmp/test-*.xml "))'

I have the next messages:

File does not exist: /tmp/test-*.xml at -e line 1

How I do it?

3 Answers3

3

There's problems with what you're trying to do:

  1. XML::Simple isn't simple. It's for simple XML. It'll mangle your XML and give inconsistent results. See: Why is XML::Simple "Discouraged"?
  2. XML is fundamentally more complicated than JSON, so there's no linear transformation. You need to figure out what'd you'd do with attributes and duplicate elements for a start.
  3. File does not exist: /tmp/test-*.xml at -e line 1 - means the file doesn't exist. So you're not going to get very far. But XMLin doesn't accept wildcards. You'll have to process one file at a time.

The first two points are solvable, provided you accept that this cannot be a generic solution - to give a moderately general solution, we'll need an example of your source XML. But it won't be a one liner.

Community
  • 1
  • 1
Sobrique
  • 52,974
  • 7
  • 60
  • 101
  • 1
    isn't simple :). I'll amend. – Sobrique Jan 31 '17 at 14:09
  • Have you ever run a phrase through an online translator and back again? And had it come out nonsense? That's what you're trying to do here. To solve your problem, you need to think about what your problem _actually is_. What needs to be in your output JSON, and what input do you need to process to get it. And then write a solution to _that_ problem, rather than a generic 'XML-to-JSON' converter, which cannot ever work. – Sobrique Jan 31 '17 at 14:21
0

You seem to be asking how to find files matching a file glob.

You could use

my @qfns = glob("/tmp/test-*.xml");

If you just want the first matching file, use

my ($qfn) = glob("/tmp/test-*.xml");

Do not use the following since glob acts an iterator in scalar context.

my $qfn = glob("/tmp/test-*.xml");  # XXX
ikegami
  • 367,544
  • 15
  • 269
  • 518
0

You can try this using glob and map functions.

perl -MJSON::Any -MXML::Simple -le'local $,="\n"; print map { JSON::Any->new()->objToJson(XMLin($_)) } glob "/path/to/my/test*.xml"'
Mohan Gutta
  • 46
  • 1
  • 5