0

Basically, what I am trying to do is to convert the XML Metadata into displaying as Structured Text, and if possible, how to not have to do this on a manual basis, if that makes sense.

This is an example of the XML:

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<SWG_Resource_Dump xmlns="generated at: Tue, 04 Jan 2022 11:59:57">
<resource>
<name>Haufa</name>
<available_date>Tue, 04 Jan 2022 11:59:59</available_date>
<resource_class>Thoranium Steel</resource_class>
<stats>
<CR>310</CR>
<CD>242</CD>
<DR>681</DR>
<HR>726</HR>
<MA>116</MA>
<OQ>903</OQ>
<SR>646</SR>
<UT>629</UT>
</stats>
<planets>
<planet>corellia</planet>
<planet>dantooine</planet>
<planet>dathomir</planet>
<planet>endor</planet>
<planet>lok</planet>
<planet>rori</planet>
<planet>talus</planet>
<planet>yavin 4</planet>
</planets>
</resource>
</SWG_Resource_Dump>

And this is an example of how I would like it structured and formatted in txt:

Name: Nygao
Available: Tue, 04 Jan 2022 11:59:57
CR: 310
CD: 242
DR: 681
HR: 726
MA: 115
OQ: 903
SR: 646
UT: 629
Planet(s): Corellia, Dantooine, Dathomir, Endor, Lok, Rori, Talus, Yavin

((Some kind of a line break))

I hope all this makes sense, and I would appreciate the feedback. Thanks!

  • I think the [xmlnamespace](https://stackoverflow.com/questions/1181888/what-does-xmlns-in-xml-mean) in your XML is not correctly defined? If it was correctly defined, you could use a tool like [xmlstarlet](http://xmlstar.sourceforge.net/) – Luuk Jan 04 '22 at 18:58

1 Answers1

0

I changed the attribute xmlns in the first tag to date. This was needed for xmlstarlet to work.

With xmlstarlet I can get:

command (tested on Windows):

xmlstarlet sel -t -o "Name: " -v //name -n -v SWG_Resource_Dump/@date -n -m //stats/* -v "name()" -o ": " -v . -n -b -o "Planet(s): " -m //planet -v . -o "," -b -n input.xml

output:

Name: Haufa
generated at: Tue, 04 Jan 2022 11:59:57
CR: 310
CD: 242
DR: 681
HR: 726
MA: 116
OQ: 903
SR: 646
UT: 629
Planet(s): corellia,dantooine,dathomir,endor,lok,rori,talus,yavin 4,

Improvements that can be done to the above:

  • The trailing , after yavin 4 should be removed.

The documentation of xmlstarlet is available here: http://xmlstar.sourceforge.net/docs.php

A (way too short) short explanation of the things used:

option description
-o "text" Outputs text
-m //planet Selects the tag planet
-v tag Prints the value of tag tag
-n Prints a newline
Luuk
  • 12,245
  • 5
  • 22
  • 33