0

I want to read text from a file which looks like:

Look up for : Bergwald Elsa-Brändström-Str
 http://www.overpass-api.de/

output:

<?xml version="1.0" encoding="UTF-8"?>
<osm version="0.6" generator="Overpass API">
<note>The data included in this document is from www.openstreetmap.org. The data is made available under ODbL.</note>
<meta osm_base="2015-07-08T07:36:02Z"/>

<node id="2505961536" lat="48.9722935" lon="8.4618593">
    <tag k="bus" v="yes"/>
    <tag k="highway" v="bus_stop"/>
    <tag k="name" v="Elsa-Brändström-Straße"/>
    <tag k="network" v="Karlsruher Verkehrsverbund (KVV)"/>
    <tag k="operator" v="Verkehrsbetriebe Karlsruhe"/>
    <tag k="public_transport" v="stop_position"/>
  </node>
</osm> 

Look up for : Bergwald Hooverstraße
 http://www.overpass-api.de/

output:

<?xml version="1.0" encoding="UTF-8"?>
<osm version="0.6" generator="Overpass API">
<note>The data included in this document is from www.openstreetmap.org. The data is made available under ODbL.</note>
<meta osm_base="2015-07-08T07:36:02Z"/> 

<node id="2505961539" lat="48.9741500" lon="8.4640651">
    <tag k="bus" v="yes"/>
    <tag k="highway" v="bus_stop"/>
    <tag k="name" v="Hooverstraße"/>
    <tag k="network" v="Karlsruher Verkehrsverbund (KVV)"/>
    <tag k="operator" v="Verkehrsbetriebe Karlsruhe"/>
    <tag k="public_transport" v="stop_position"/>
  </node>
</osm>

At first I have to to extract the string which comes after Look up for :. Then I have to extract the part belonging to the first extracted string which starts at the tag osm version= and finish at the tag osm. I want to parse this part of with a xml parser.

I could not find any solution where I can match each extracted string values after Look up for : to its belonging output.

Does anyone have any ideas?

Best regards, Nazar

dingo_d
  • 11,160
  • 11
  • 73
  • 132
Nazar Medeiros
  • 437
  • 4
  • 10

2 Answers2

0

Can't this be solved by simply reading in the file (here's how) and then looking splitting the whole string on "Look up for : ". Then you can look for "Look up for *$". You can even do it without regex if you just find "Look up for : " and then take take a substring from the end of this to the end of the line.

Do the same for "output:" If you split on this you will end up with the xml all on it's own (either use String.split or just substring). Then you just parse it.

Community
  • 1
  • 1
Astrogat
  • 1,617
  • 12
  • 24
0

I just found a working solution. Here it is...

FileInputStream fs= new FileInputStream("C:/Users/kca/Desktop/testFile.txt");
    BufferedReader br = new BufferedReader(new InputStreamReader(fs));

    String haltestellenName = "";

    try {
        StringBuilder sb = new StringBuilder();

        String line = br.readLine();

        while (line != null) {

            StringBuilder response = new StringBuilder();

            line = br.readLine();
            sb.append(line);

            if(line.contains("Look up for :")) {

                haltestellenName = line;
                haltestellenName = line.split(":")[1];

            }

            if(line.contains("<osm version=")) {
                response.append(line);                
                while(!(line.contains("</osm>"))) {

                    line = br.readLine();
                    response = response.append(line);
                    response = response.append("\n");

                }

            }

            if(line.contains("</osm>")) {

                System.out.println("Look up for " + haltestellenName);
                System.out.println("with the matching output " + response); 
                System.out.println("\n");
            }



            sb.append(System.lineSeparator());
            //System.out.println(line);
        }
     String everything = sb.toString();  

    } finally {
        br.close();
    }
Nazar Medeiros
  • 437
  • 4
  • 10