0

To parse an input text file and generate a) an XML file and b) an SVG (also XML) file.

The input text file (input.txt) contains the description of a number of produce distribution centers and storage centers around the country. Each line describes either a single distribution center (dcenter) or a storage center, each with a number of properties; each property name (code for example) is separated by its value with a =.

Example (input.txt)

dcenter: code=d1, loc=San Jose, x=100, y=100, ctype=ct1
dcenter: code=d2, loc=San Ramon, x=300, y=200, ctype=ct2
storage: code=s1, locFrom=d1, x=50, y=50, rtype=rt1
storage: code=s2, locFrom=d1, x=-50,y=100, rtype=rt1

The desired Output of the program:

Output 1

<?xml version="1.0"?>
<dcenters>
<dcenter code="d1">
<loc> San Jose </loc>
<x> 100 </x>
<y> 100 </y>
<ctype> ct1 </ctype>
</dcenter>
<storage code="S1">
<locFrom> d1 </locFrom>
<x> 150 </x>
<y> 150 </y>
<rtype> rt1 </rtype>
</storage>
<storage code="S2">
<locFrom> d1 </locFrom>
<x> 50 </x>
<y> 200 </y>
<rtype> rt1 </rtype>
</storage>

Please help me with the program. I will really appreciate.

GManNickG
  • 494,350
  • 52
  • 494
  • 543
  • What's the actual problem you're having? That seems pretty straightforward. – Carl Norum Oct 02 '09 at 21:53
  • I read this and thought to myself hmmm...I wonder if it's possible to run XSLT in reverse. Turns out some crazy person actually implemented this. http://www.reversexsl.com/j/ – Paul Abbott Oct 02 '09 at 22:11
  • Actually i need a program which can parse an input text file which i shown above as input.txt into xml file as output1. please help me with this. –  Oct 03 '09 at 00:10

1 Answers1

0

Suppose the input is in string s; either from direct assignment or from file.read:

s="""dcenter: code=d1, loc=San Jose, x=100, y=100, ctype=ct1
dcenter: code=d2, loc=San Ramon, x=300, y=200, ctype=ct2
storage: code=s1, locFrom=d1, x=50, y=50, rtype=rt1
storage: code=s2, locFrom=d1, x=-50,y=100, rtype=rt1"""

Then you can this:

print '<?xml version="1.0"?>'
print "<dcenters>"
for line in s.splitlines():
    type, fields = line.split(":")
    params = fields.split(",")
    code = params[0].split("=")[1].strip()
    print '<%s code="%s">' % (type, code)
    for p in params[1:]:
        ptype, pvalue = p.strip().split("=")
        print '<%s> %s </%s>' % (ptype, pvalue, ptype)
    print '</%s>' % type
print "</dcenters>"

Not sure why d2 is missing from your sample output; I assume that's by mistake.

Martin v. Löwis
  • 124,830
  • 17
  • 198
  • 235
  • Thanks for replying Martin..I really appreciate ur help. Actually i need a program which can parse an input text file which i shown above as input.txt into xml file as output1. please help me with this. –  Oct 03 '09 at 00:12
  • John, that IS a program that will help. It is written in Python. If you can't figure out the rest from there, I would suppose you don't really know what you are doing. This sounds like homework, and I'm kind of suprised that you got an answer to it without first trying yourself. – Josh Smeaton Oct 17 '09 at 03:00