Java从文件中获取特定内容

时间:2015-07-13 07:34:59

标签: java xml-parsing readfile extract-value

我想从文件中读取文字,如下所示:

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>

首先,我必须提取查找:之后的字符串。然后我必须提取属于第一个提取的字符串的部分,该部分从标签 osm version = 开始,并在标签 osm 处完成。我想用xml解析器解析这部分内容。

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

有没有人有任何想法?

祝你好运, 纳扎尔

2 个答案:

答案 0 :(得分:0)

这不能通过简单地读入文件(here's how)然后在“查找:”中分割整个字符串来解决。然后你可以寻找“查找* $”。你甚至可以在没有正则表达式的情况下执行它,如果你只是找到“查找:”然后从该结尾处取一个子字符串到该行的末尾。

对“output:”执行相同的操作:如果您对此进行拆分,您将最终使用xml all(使用String.split或仅使用子字符串)。然后你只需要解析它。

答案 1 :(得分: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();
    }