Java中使用Jdom解析和编辑XML

发布于 2024-12-11 00:05:54 字数 297 浏览 0 评论 0原文

我为这个问题苦苦挣扎了一个星期。我尝试用java编写一个可以编辑电子书的程序。我选择的电子书格式是fictionbook,其数据包含在类似xml 的文件中。我可以打开此文件并在 JTextComponent 中生成源视图。我的编辑器预览和源代码视图中有两个切换按钮。他们使用相同的 JTextComponent 进行显示。当用户单击源视图时,它会显示所选的文件源(纯 xml)。我通过解析我需要显示的数据来从此源生成预览。如果我更改源视图中的某些内容,它也会显示在预览中,因为它是从那里生成的,我的问题是如何向后进行此操作。因此,如果我在预览中更改某些内容,它也会在源代码中更改。

I'm struggling with this problem for a week. I try to write a program in java that can edit ebooks. My choosed ebook format is fictionbook which contains its data in an xml like file. I can open this file and generate a sourceview in a JTextComponent. There are two togglebuttons in my editor preview and sourceview. They using the same JTextComponent for display. When the user clicks on the sourceview it display the choosen file source(plain xml). I generate the preview from this source by parsing the data i need to display.If i change something in the sourceview it will be displayed in the preview too because its generated from there, my problem is how can i made this backwards. So if i change something in preview it will change in the source too.

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

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

发布评论

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

评论(1

云淡月浅 2024-12-18 00:05:55

很难将此视为特定于 JDOM 的问题。底层的表示几乎可以是任何东西。通常预览就是这样:无法直接编辑的最终结果的视图。如果您希望能够编辑源代码并通过所见即所得视图进行编辑,则需要研究模型-视图-控制器模式。

底层的 XML 将是您的模型。 XML 编辑器视图和 WYSIWYG 视图(您当前的预览)都将是视图和控制器。目前,这仅适用于 XML 编辑器,而预览只不过是一个视图。

从预览中更改底层 XML 听起来很困难,除非可以以某种方式识别每个节点(例如通过 id 属性)。您将需要在预览中添加一些额外的基础数据才能在另一个方向上进行耦合。

我不确定 JDOM 文档是否是最适合您的目的的模型。也许您最好使用 JAXB 之类的东西来使用简单的 JavaBean 来表示模型,然后可以将其编组为 XML 并从 XML 中解组。所以你会得到:

                    model: JavaBeans with JAXB annotations
                      ^
                      |
    (directly) +--------------------------------------+ (through JAXB)
               |                                      |
WYSIWYG editor: view + controller             XML editor: view + controller

但这只是我的第一印象。调查现有的编辑器以找出执行此操作的好方法。也许检查 IDE 插件/模块可能会很有趣,因为 IDE 通常允许多种类型的编辑器更改底层数据模型。

It's kind of hard to see this as a JDOM-specific question. The underlying representation could be pretty much anything. Normally a preview is just that: a view of the end result that wouldn't be directly editable. If you wish to be able to edit both the source and through a WYSIWYG view, you'll need to investigate the model-view-controller pattern.

The underlying XML would be your model. Both the XML editor view and the WYSIWYG view (your current preview) would then be views AND controllers. Currently this is only the case for your XML editor, whereas the preview is nothing but a view.

Changing the underlying XML from the preview sounds difficult, unless each node can somehow be identified (like via an id attribute). You'll need some additional underlying data in your preview to do this coupling in the other direction.

I'm not certain a JDOM Document is the best model for your purposes. Maybe you're better of using something like JAXB to have simple JavaBeans to represent the model which can then be marshalled to XML and unmarshalled from XML. So you get:

                    model: JavaBeans with JAXB annotations
                      ^
                      |
    (directly) +--------------------------------------+ (through JAXB)
               |                                      |
WYSIWYG editor: view + controller             XML editor: view + controller

But that's just my first impression. Investigate existing editors to find out good method for doing this. Maybe checking out IDE plugins/modules could be interesting, since IDEs typically allow multiple types of editors to change an underlying data model.

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