0

I would like to make a java code that takes this .txt and outputs it as .xml . XML file should be fellow pattern e.g

Text file:

DATA  rtr Deme_MS_GDA_DRGH R_2LOAM_OML13 R_OML13_OLMUA

LINE  R_LG_OML13_2LOAM _LINETYP_20    0.500    0.250  0.000  0.000  0.000 0.000   0.000 0.000 0.000

SHUT  MT -1 R_2LOAM_OML13_LSMT -1 e  0.000 NT -1 R_2LOAM_OML13_NTR -1 a    0.000 MT -1 R_2LOAM_OML13_QK R_2LOAM_GG_____GG____ e    0.000

MASS   0x0

SHORT  0x0 -1 -1 -1 -1

And Output in XML should be like this :

<?xml version="1.0"?>

<Field>
<DATA  feldsimtyp="rtr" feldtoptyp="Deme_MS_GDA_DRGH" feld="R_2LOAM_OML13"  gegenfeld="R_OML13_OLMUA">            
<LINE          name="R_LG_OML13_2LOAM" leitungstyp="_LINETYP_20" leitungslaenge="0.500" grenzstrom="0.250" unsymL1="0.000"  unsymL2="0.000"  unsymL3="0.000" resistanz="0.000" reaktanz="0.000" betriebskapazitaet="0.000" erdkapazitaet="0.000"/ >
<SHUT>
   <SHUT typ="MT" meldung="-1" name="E_AOLM2_OLM14_VSLT" anschlussknoten="-1" einschaltzustand="e" kurzschlussstrom=0.000"/>    
   <SHUT typ="NT" meldung="-1" name="R_2LOAM_OML13_NTR" anschlussknoten="-1" einschaltzustand="a" kurzschlussstrom=0.000"/>
   <SHUT typ="MT" meldung="-1" name="R_2LOAM_OML13_QK" anschlussknoten="R_2LOAM_GG_____GG____" einschaltzustand="e" kurzschlussstrom=0.000"/>   
</SHUT>
  <MASS  bitmsake="0x0">
  </MASS> 
<SHORT  bitmaske="0x0" schalter1="-1" schalter2="-1" schalter3="-1" schalter4="-1"/>
</Field>
Luke Woodward
  • 63,336
  • 16
  • 89
  • 104
  • you may try with XSL and transform using xalan processor into XML – AzizSM Aug 10 '12 at 09:36
  • The structure seems pretty straightforward. The easy way is to read the file line by line, check what the identifier is (first word) and create the specified element(s). As for how to create a valid xml take a look at this example [site](http://www.javazoom.net/services/newsletter/xmlgeneration.html) – George Karanikas Aug 10 '12 at 09:37

2 Answers2

0

As is, I think there is no straight forward way of doing it.

What you could try would be to iterate over the file, parse it yourself with some custom logic and then, use something such as StAX to build the XML from scratch.

npinti
  • 51,780
  • 5
  • 72
  • 96
0

Seems like a case for regular expressions. Those are supported by java through the Pattern class.

Please see the following example and try it.

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class RegexDemo {

public static void main(final String[] args) {
    final String data = "#DATA  rtr Deme_MS_GDA_DRGH R_2LOAM_OML13 R_OML13_OLMUA";
    final Pattern PATTERN = Pattern.compile("^#DATA\\s+(\\w+)\\s+(\\w+)\\s+(\\w+)\\s+(\\w+)$");
    final Matcher matcher = PATTERN.matcher(data);
    if (matcher.matches()) {
        final String xmlData = String.format("<DATA a='%s' b=%s c=%s d=%s />", matcher.group(1), matcher.group(2), matcher.group(3), matcher.group(4));
        System.out.println(xmlData);
    }

}

}

ChriWeis
  • 207
  • 2
  • 7