5

I downloaded a catalog and it is in XML format. How do change it from code to human-readable form, and ideally save it as a plain text file? I've never worked/seen these before; I tried messing with xslproc as it was recommended on here for somewhat similar things, but couldn't get it to work. I successfully loaded it in textWrangler, which formats it correctly (indents/colors/etc) in XML, but i don't see how I can actually 'run' it to print its contents. I also tried Excel, Word, Notepad, and just ended up with errors.

If that wasn't clear, i have stuff like this:

      <TD>51.9029244701</TD>
      <TD>47.0082067303</TD>

and I want it like this:

51.9029244701
47.0082067303

I want to make a text file with this catalog's information.

2
3

Late answer here: Ubuntu repository has a very good utility called xmlto that could help you. It converts xml to a variety of formats, including plain text, epub, pdf.

Online, there is Oxgarage which has many conversion options.

2

I wrote up a very simple Python script that will read in an xml file, and output its contents into another file:

import sys
inFile = open(sys.argv[1], 'r')
outFile = open(sys.argv[2], 'w')    
read = True
for i in inFile.read():
    if i == '<':
        read = not read
    if read:
        outFile.write(i)
    if i == '>':
        read = not read

Save this as readxml.py and then call it from the shell like this: $ python readxml.py input.xml output.txt . It's really rudimentary, so it might not be what you're looking for exactly, but it's something!

1
  • It's looking like I might have to take this path; messing around with the all the header lines won't be fun though - so for now, I'm going to hold out for an easier solution haha
    – deedsy
    Aug 1 '14 at 19:11
2

Try this:

grep "<TD>" yourfile.xml | awk -F "TD" '{gsub(">|</","",$0); print $2;}'

the output will be:

51.9029244701
47.0082067303

grep select xml TD tag, awk use TD as separator and remove > and </ from $2 field.

In order to select all node, try xslt transformation:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
   <xsl:output method="text" indent="yes"/>
   <xsl:template match="node()">
      <xsl:value-of select="." />
   </xsl:template>
</xsl:stylesheet>

This will output content of all node in your xml file.

1
  • thanks, but I guess I should have specified only a part of the file looks like the example I posted. The header and other sections get really messy. I don't know if this will work for the entire file, but maybe it can for a decent chunk of it - I'll have to see.
    – deedsy
    Aug 2 '14 at 3:04
1

This is easier than you think actually - XML elements have 4 'things' - a name, attributes, children and 'content'.

So you can iterate all the content elements and just print them (with or without a linefeed)

#!/usr/env/perl

use strict;
use warnings;

use XML::Twig;

my $twig = XML::Twig -> parsefile ( 'your_file');
print $_ -> text."\n" for grep { $_ -> is_pcdata } $twig -> get_xpath('//*');

This fishes out all the 'content' (pcdata) nodes, and prints them - one per line.

But really, when it comes to XML "human readable" is sort of the point. Why not just pretty print it and leave it at that, so the human reading it can ... well, read it, and not lose the data in the process.

You can reformat it if you like:

$twig -> set_pretty_print ('indented_a'); 
$twig -> print;

Which is a pretty good approximation of 'human readable'.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.