我需要编写一个基于 swing 的编辑器,它可以打开指定的 xml 文件,其中包含我必须在
和
标记之间使用的文本,但文件中还有其他标签。文件中还有其他无用的信息。我不需要显示它们,但需要保留它们。我只需要显示
JTextComponent
中提到的标签内的文本,并让用户修改它并以某种方式将更改写回底层 xml 源。 xml 文件中标签的位置因文件而异,并且一个文件中有多个
标签,我应该显示所有标签的内容并使其可编辑立刻。你怎么认为?完成上述任务的最佳方法是什么?
I need to write a swing based editor that can open specified xml files, which contains the text i have to use between a <p>
and </p>
tag, but there are other tags in the file too. There are other useless informations in the file.I don't need to display them, but it needs to be preserved. I need to dispay only the text inside the mentioned tags in a JTextComponent
and let the user modifying it and somehow write back the changes to the underlying xml source. The positions of the tags in the xml file will vary from file to file and there are more than one <p>
tags in a file, and i should display and make editable the content from all of them at once. What do you think? Which is the best way to accomplish the above task?
发布评论
评论(3)
如果您想要一个快速的编程解决方案,只需将 XML DOM 读入 JTree 即可。以下是您可以在网上找到的众多示例之一:
http://www.developer.com/xml/article.php/3731356/Displaying-XML-in-a-Swing-JTree.htm
如果如果您正在寻找开源 XML 编辑器,您可能需要考虑 Amaya:
http://www.w3。 org/Amaya/
PS:
您可能知道,Swing 的“JTree”实现是 MVC 的最大化 - 您可以轻松地调整任何示例来过滤树模型的内容(即过滤/修改 DOM 内容源),或更改外观(即修改 JTree 的外观和/或行为)。
If you want a quick'n'dirty programmatic solution, just read your XML DOM into a JTree. Here's one of many, many examples you can find on the web:
http://www.developer.com/xml/article.php/3731356/Displaying-XML-in-a-Swing-JTree.htm
If you're looking for an open source XML editor, you might want to consider Amaya:
http://www.w3.org/Amaya/
PS:
As you're probably aware, Swing's "JTree" implementation is MVC to the max - you can easily adapt any example to filter the contents of the tree model (i.e. to filter/modify your DOM content source), or to change the appearance (i.e. modify your JTree's appearance and/or behavior).
http://java-sl.com/xml_editor_kit.html
您可以使用该套件作为您的项目的基础。添加
DocumentFilter
以允许仅编辑纯文本。http://java-sl.com/xml_editor_kit.html
You can use the kit as the basis for your project. Add a
DocumentFilter
to allow editing only the plain text.