0

我想从一个看起来像这样的文件中读取文本:

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>

首先,我必须提取Look up for :之后的字符串。然后我必须提取属于第一个提取字符串的部分,该字符串从标记osm version=开始并在标记osm处结束。我想用 xml 解析器解析这部分。

在查找 :之后,我找不到可以将每个提取的字符串值与其所属输出匹配的任何解决方案。

有没有人有任何想法?

最好的问候, 纳扎尔

4

2 回答 2

0

我刚刚找到了一个可行的解决方案。这里是...

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();
    }
于 2015-07-13T08:23:30.583 回答
0

这不能通过简单地读取文件(这里是如何)然后在“查找:”上拆分整个字符串来解决。然后您可以查找“查找 *$”。如果您只找到“查找:”,然后从该行的末尾取一个子字符串,您甚至可以在没有正则表达式的情况下执行此操作。

对“输出:”执行相同操作:如果您对此进行拆分,您最终会得到自己的 xml(使用 String.split 或仅使用子字符串)。然后你就解析它。

于 2015-07-13T08:01:22.623 回答