gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 11:55:55 26 4
gpt4 key购买 nike

<小时/>

> <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;

----Make database call and store CAR and DESCRIPTION information into Database.

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

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

最佳答案

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

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

如果您没有根元素,则此异常有点误导。发生的情况是解析器假设它命中的第一个标签是根元素。当它遇到该假定根元素的 close 之外的内容时,它会翻转并死亡。应该清楚的是,解析器失败是因为它试图构建一棵树,但如果没有根,它就会有悬空标签,无法附加到任何东西。

关于java - 从文件中提取多个 xml 标记之间的数据的最佳方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28444583/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com