在 Java 中使用 xalan 搜索 XML 文件

发布于 2024-08-18 06:10:30 字数 109 浏览 5 评论 0原文

我需要编写一个 java 应用程序,在标签和来自许多 xml 文件的实际数据中进行关键字搜索。从我的在线研究中,我感觉我必须使用 xalan,但我不知道如何使用它或它的作用。有人能指出我正确的方向吗?谢谢

I need to write a java application that does a keyword search within the tags and the actual data from many xml files. From my research online I get the feeling i have to use xalan, but I can't figure out how to use it or what it does. Could somebody point me in the right direction? Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

夜血缘 2024-08-25 06:10:30

您需要做的第一件事是决定您实际要搜索的数据。您说“在标签和实际数据内”——这是否意味着您将对元素名称进行关键字搜索?或者元素名称和其中的内容?

根据搜索查询的复杂程度,您可能需要转向真正的搜索引擎,例如 卢森。但是,我要说的是,在执行此步骤之前,您需要仔细考虑计划如何搜索,以便构建适当的索引。

如果您的搜索要求更简单,您可以将文档加载到 DOM 中并使用 XPath 。我建议在转向 Lucene 之前尝试一下。

你不需要 Xalan; JDK 附带了 XML 解析器和 XPath 求值器。我写了几篇关于使用它们的文章:(解析 ), (xpath)。

The first thing you need to do is to decide what data you're actually going to search. You say "within the tags and actual data" -- does that mean that you'll do a keyword search for an element name? Or an element name and content within it?

Depending on how complex your search queries are, you'll probably want to turn to a real search engine, like Lucene. I will say, however, that before you take this step you need to give a lot of thought to how you plan to search, so that you build an appropriate index.

If your search requirements are simpler, you could load the documents into a DOM and use XPath. I'd suggest trying this out before moving to Lucene.

You don't need Xalan; the JDK comes with XML parsers and an XPath evaluator. I've written a couple of articles on using them: (parsing), (xpath).

疑心病 2024-08-25 06:10:30

Xalan 是一个 XSLT 处理器:它使您能够编写 XSL 样式表,将源 XML 文档转换为其他内容。

当然可以编写一个 XSL 转换,然后搜索转换的结果。

另一种选择是使用 XML 解析器解析文档,然后使用 Lucene:请参阅 使用 Digester 和 Lucene 解析、索引和搜索 XML 文档

您可能还想使用 XPath。这完全取决于您到底想要实现什么目标。

Xalan is an XSLT processor: it enables you to write an XSL stylesheet that will transform your source XML document into something else.

Sure may write an XSL transform and then you search the result of the transform.

Another option is to parse the document with an XML parser and then use Lucene: see Parsing, indexing, and searching XML documents with Digester and Lucene.

You may also want to use XPath. It all depends on what exactly you want to achieve.

丶视觉 2024-08-25 06:10:30

听起来您正在寻找 Java 的 XPath 实现。这允许您构造一个搜索表达式并将其应用于一个或多个 XML 文档(通常必须已对其进行解析)。 Xalan 是一种选择,但还有其他选择。从 Java 5 开始的 Java 版本已包含 XML 解析和 XPath 功能。如果您使用的是最新版本的 Java,并且想要简单地解析和搜索一组 XML 文档,那么除了 Java SDK 之外,您可能不需要任何东西。

请参阅这篇文章,了解“开箱即用”的 XPath 功能的良好(但有些过时)概述:http://www.ibm.com/developerworks/library/x-javaxpathapi.html

I sounds like you are looking for an XPath implementation for Java. This allows you to construct a search expression and apply it to one or more XML documents (which generally have to have been parsed). Xalan is one option, but there are others. Versions of Java starting with Java 5 have included XML parsing and XPath capabilities. If you are using a recent version of Java, and want to simply parse and search through a set of XML documents, then you likely need nothing besides the Java SDK.

See this article for a good (but somewhat dated) overview of the XPath capabilities that come "out of the box": http://www.ibm.com/developerworks/library/x-javaxpathapi.html

我爱人 2024-08-25 06:10:30

请参阅这篇文章了解如何使用 contains 进行搜索() XPath 函数。

至于如何执行 XPath 查询的示例,我建议查看 Java XPath 文档。下面是他们提供的示例代码:

XPath xpath = XPathFactory.newInstance().newXPath();
String expression = "/widgets/widget";
InputSource inputSource = new InputSource("widgets.xml");
NodeSet nodes = (NodeSet) xpath.evaluate(expression, inputSource, XPathConstants.NODESET);

这将加载文件 widgets.xml 并返回与表达式匹配的所有节点的 NodeSet

See this SO post on how to do a search using the contains() XPath function.

As for an example on how to do an XPath query, I suggest looking at the Java XPath documentation. Here's the example code they provide:

XPath xpath = XPathFactory.newInstance().newXPath();
String expression = "/widgets/widget";
InputSource inputSource = new InputSource("widgets.xml");
NodeSet nodes = (NodeSet) xpath.evaluate(expression, inputSource, XPathConstants.NODESET);

This would load the file widgets.xml and return a NodeSet of all nodes matching the expression.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文