从文件中的几个xml标记之间提取数据的最佳方法

时间:2015-02-11 00:37:31

标签: java xml eclipse string stringbuffer


> <DOC>
> <Car> Zen </Car> 
> <Description> This is a bla bla model. which is a bla
> bla thisnf dsgs
> sdfsgssssssssssssssssssssssssssssssssssssssssssttttttttttttttwqqqqqqqqq
> dsgdsdsssssssssssegsegsdgsdgsdsssssssssssssssssssssttttttttttttt
> sdgssddddddddddddddddddddddddddddddddddddddddddddddsdddddddddwwww
> dgdssdddddddddddddddddddddddddddddsssssssssssssssssssssssswwwwwwwwwwww
> gdgdsssssssssssssssssssssssssssssssssssssssssssssssssseeeeeeeeeeeeee
> gddsssssssssssssssssssssssseeeeeeeeeeeeeeeeeeeeeeeeeeeeeeqqqqqqqqqqq
> gsdsssssssssssssssssssssssssssssseqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq
> dsssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssss
> arrwerfkafjsdfsojfiosjfiosdfoisdoifjsdoifjiosdjfosdj</Description>
> <Year> 2015 </Year> <Color> Red </Color> 
> <Engine> afsdf </Engine>
> </DOC>

---更多标签----

> <DOC>
> <Car> Zen1 </Car> <Description> This is the second text tag which is a
> bla bla thisnf dsgs
> sdfsgssssssssssssssssssssssssssssssssssssssssssttttttttttttttwqqqqqqqqq
> dsgdsdsssssssssssegsegsdgsdgsdsssssssssssssssssssssttttttttttttt
> sdgssddddddddddddddddddddddddddddddddddddddddddddddsdddddddddwwww
> dgdssdddddddddddddddddddddddddddddsssssssssssssssssssssssswwwwwwwwwwww
> gdgdsssssssssssssssssssssssssssssssssssssssssssssssssseeeeeeeeeeeeee
> gddsssssssssssssssssssssssseeeeeeeeeeeeeeeeeeeeeeeeeeeeeeqqqqqqqqqqq
> gsdsssssssssssssssssssssssssssssseqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq
> dsssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssss
> arrwerfkafjsdfsojfiosjfiosdfoisdoifjsdoifjiosdjfosdj</Description>
> <Year> 2015 </Year> <Color> Red </Color> <Engine> afsdf </Engine>
> </DOC>

---更多标签----

我有几个文件,每个文件中都有很多这样的标签。 在每个标签和标签之间提取数据的最佳方法是什么? 这是我的方法

> for (File file : files) { 
> System.out.println(file.getName());   
> if
> (file.isFile()) {         
> try {
>       BufferedReader bufferReader = new BufferedReader(new FileReader(file));             >       String line; 
>       XContentBuilder builder = null;
>       StringBuffer sb = null; 
>       String descrption= null; 
>       String car =null;   
>       Boolean within_description = false; 
>           String Car= "";
>           while ((line = bufferReader.readLine()) != null) {
>               // System.out.println(line);
>               if (line.equals("<DOC>")) {
>                   builder = jsonBuilder();
>                   sb = new StringBuffer();
>                   continue;
>               } 
>               
>       
>               if (line.contains("<CAR>")) {
>                    
>                   String remove_front_space = line.replaceAll("<CAR>",
>                           "");
>                   car= remove_front_space .replaceAll("</CAR>",
>                           "").trim();
>                   builder = builder.startObject().field("CARR",
>                           car);
>                   continue;
>               }
>               if (line.equals("</DESCRIPTION>")) {
>                   within_description = false;
>                   continue;
>               }
>               if (within_description) {
>                   sb.append(line);
>                   continue;
>               }
>               if (line.equals("<DESCRIPTION>")) {
>                   within_description = true;
>                   continue;
>               }
>               if (line.equals("</DOC>")) {
>                   // JSONifying the string data
>                   text_toadd = sb.toString();
>                   builder = builder.field("text", text_toadd)
>                           .endObject();
>                   sb = null;
  

----进行数据库调用并将CAR和描述信息存储到数据库中。

>                 }     
>           bufferReader.close(); 
>           String json = builder.string(); 
>           System.out.println(json);
> 
>       } catch (IOException e) {           
>           e.printStackTrace();        
>       }
>   } }

任何建议都可以。 提前谢谢!

1 个答案:

答案 0 :(得分:1)

有一篇优秀的帖子here,讨论了如何使用DOM解析器将XML文件提取到树中。需要记住的一点是,必须在XML文件中包含根元素。如果不这样做,将导致以下SAXParseException:

org.xml.sax.SAXParseException: The markup in the document following the root element must be well-formed.

如果您没有根元素,则此异常有点误导。发生的事情是解析器假定它命中的第一个标签是根元素。当遇到此假定根元素的 close 之外的内容时,它会翻转并死掉。应该很清楚,解析器失败是因为它正在尝试构建一个树,但是如果没有root,它就会有悬挂的标签,这些标签不能附加到任何东西上。