使用Java将模式xml与数据xml合并 [英] Merging schema xml with data xml with Java

查看:95
本文介绍了使用Java将模式xml与数据xml合并的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我希望,基于两个xml:

1. ui.xml:根据属性中的变量/条件定义布局,例如as:

展开 | 选择 | Wrap | 行号

解决方案

在Java中读取两个XML作为DOM树(参见org.w3c .dom.Document)。

然后将它们合并到生成的DOM树中。

(你必须通过自己的java代码提供合并逻辑。使用XPath非常有帮助这样做。)

然后将生成的DOM树保存为XML文件(使用TransformerFactory到得到一个变换器实例,然后使用它的transform()方法)。


所以我应该在拥有一个dom-tree之后,在java中使用xpath来选择所有属性(根据我的例子,所有''标题''属性),在那些属性中我应该编写xpath代码,并在我的第二个(数据)dom-tree上评估它?

然后,将属性'的值替换为评估的那个?

我可以看到我如何提取信息,但是我怎么能在xpath中告诉if语句,这样如果它发现某个值是''X'',则分配'' Y''到那个属性。

我不确定它是否符合我的需求。

你能否更具体一点?我不太确定合并过程中的评估是如何发生的。



你做对了。只需将DOM中的属性值作为String获取即可。在您的示例中,您将存储if data / gender =''female'':Ms .;否则:先生在变量(例如)中命名为scriptString。我不认识这种语言。但是我们假设它是javascript或xpath。

如果是javascript,你可以使用脚本引擎来评估这个表达式:

展开 | 选择 | Wrap | 行号

Hello everyone,
I wish, based on two xml:
1. ui.xml: defines a layout, based on variables/conditions in its attributes, such as:

Expand|Select|Wrap|Line Numbers

解决方案

Read in both XMLs as DOM-tree in Java (see org.w3c.dom.Document).
Then merge them into a resulting DOM-tree.
(You must provide the merge logic by your own java code. Using XPath is very helpful doing it.)
Then save the resulting DOM-tree as an XML file (using TransformerFactory to get a transformer instance and then using its transform() method).


So I should, after having a dom-tree, use xpath in java to select all attributes (according to my example, all ''title'' attributes), and in those attributes i should write xpath code, and evaluate it on my second (data) dom-tree?
then, replace the attribute''s value to the evaluated one?
I can see how I can pull information, but how can I tell in xpath if statements, so that if it found that a certain value is ''X'', assign ''Y'' to that attribute.
Im not sure it will fit to my needs.
Could you be a bit more specific? Im not quite sure how the evaluation during merge process occurs.

I


You got it right. Just get the value of the attribute from the DOM as a String. In your example you would store "if data/gender = ''female'': Ms.; else: Mr." in the variable (for example) named scriptString. I don''t recognize this language. But let us assume it is javascript or xpath.
If it is javascript, you could use a scripting engine to evaluate this expression:

Expand|Select|Wrap|Line Numbers


这篇关于使用Java将模式xml与数据xml合并的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆