【问题标题】:Extract additional information from CSV/XML file in Java Code从 Java 代码中的 CSV/XML 文件中提取附加信息
【发布时间】:2014-08-03 05:20:25
【问题描述】:

我有一个问题要问你。

我有一个 XML 文件(或 CSV 文件):

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<City>
    <Code>LO</Code>
    <Name>London</Name>
</City>

我想在 Java 代码中从中提取附加信息(例如,作者、描述、创建者、评论、格式、内容类型等)。

我读过这个类似的问题,但提取的是从 Excel 文件到 Java 代码:How to set Author name to excel file using poi

如果我输入文件名(例如 test.csv 或 test.xml),我想输入附加信息(例如 System.out.println(getAuthor))。

谁能帮帮我?

【问题讨论】:

  • 您能否更新您的示例 XML 以包含您希望提取的标签?
  • 我想提取附加信息,这些信息不存在于示例 XML 中,但如果您在文件上单击鼠标右键并选择“属性”,就会存在这些信息。有“NameFile”、“TypeFile”、“DimensionFile”、“CreatorFile”等信息。

标签: java xml csv information-retrieval information-extraction


【解决方案1】:

这些信息不在文件本身内,它只包含其内容(如您的 XML 字符串)。它们取决于操作系统(您使用的是哪一个?)。而且有点不清楚您在寻找什么。所以这就是你提到的:

作者

Path path = Paths.get("C:/Users/Thomas/workspace_eclipse_java/Test/javassist-3.12.1.GA.jar");
FileOwnerAttributeView owner= Files.getFileAttributeView(path, FileOwnerAttributeView.class);
System.out.println("owner: " + owner.getOwner().getName());

说明

我不知道这应该是什么。在 Windows 或 Linux 上从未见过。

创作者

你又是指作者吗?

评论

我不知道这应该是什么。在 Windows 或 Linux 上从未见过。

格式

检查文件扩展名

内容类型

检查文件扩展名或看看里面。

一般

通常您可以通过以下方式检查可用的内容:

FileSystem fileSystem = FileSystems.getDefault();
Set<String> fileSystemViews = fileSystem.supportedFileAttributeViews();
for (String fileSystemView : fileSystemViews)
    System.out.println(fileSystemView);

【讨论】:

  • 谢谢托马斯。如前所述,我想提取示例 XML 中不存在的附加信息,但如果您在文件上单击鼠标右键并选择“属性”,则会出现这些信息。有“NameFile”、“TypeFile”、“DimensionFile”、“CreatorFile”等信息。例如,如果您在 Excel 文件上右键单击鼠标并选择“属性”,您会注意到属性列表,如作者、评论等。我想要相同的属性也是 CSV 或 XML 文件。对不起,我不会说英语!!!
  • 谢谢!我使用 FileOwnerAttributeView 和 BasicFileAttributeView 来获取此信息。特别是,我使用了 BasicFileAttributeView 类的“readAttributes”方法来获取“lastModifiedTime”和文件的大小,并使用“FileOwnerAttributeView”方法来获取文件的所有者。
猜你喜欢
  • 2019-07-13
  • 1970-01-01
  • 2015-09-03
  • 1970-01-01
  • 2011-11-21
  • 1970-01-01
  • 2021-08-29
  • 1970-01-01
  • 2020-12-20
相关资源
最近更新 更多