XML和Java帮助 [英] Help with XML and Java

查看:69
本文介绍了XML和Java帮助的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Piccolo2D中的软件包创建ZUI应用程序.

piccolo2D的工作方式是创建节点,然后将其显示在面板中.您可以将其他节点添加到面板中,也可以将其添加到刚创建的节点中,依此类推以构成层次结构.

每个节点都有几个属性,通常,您可以在创建节点时对其进行硬编码.但是我要做的是将这些属性值存储在XML文件中,解析这些值,然后使用这些值创建节点.

我使用SAX解析器从XML文件中读取值,然后在控制台中打印出值,这不是问题.问题实际上是使从XML文件读取的值进入节点的属性.

因此,例如,当您创建节点时,可以通过添加以下代码(填写值)来实现:

I am creating a ZUI application using the packages from Piccolo2D.

The way piccolo2D works is that you create nodes, which are then displayed within the panel. You can add additional nodes to the panel or add it to the node you have just created and so on to make a hierarchy structure.

Each node has several properties and normally you can hard-code them in when you create the node. What I want to do however is store these property values in an XML file, parse these values in and then create the node with these values.

I got reading in the values from the XML file using the SAX parser, and this then prints out the values within the console, that isn''t the problem. The problem is actually getting the values read from the XML file into the properties of the node.

So for example when you create a node, you do so by adding this code (filling in the values):

PNode node1 = PPath.createRoundRectangle(x, y, width, height, arc, arc);



但是,我想从SAX解析器中读取值并插入节点的属性值,这样做时遇到问题.

有人可以帮我吗?



However I want to get the values read in from the SAX parser and insert the property values for the node, and I am having problems doing this.

Can anyone help me?

推荐答案

嗯...为什么你不尝试类似(简化)的东西:

Hmm... Why do you not try something like (simplified):

int x_fromXml = getAsInt(xmlElement_x);
int y_fromXml = getAsInt(xmlElement_y);

int width_fromXml = getAsInt(xmlElement_width);
int heigth_fromXml = getAsInt(xmlElement_hight);

Arc arc1_fromXml = getAsArc(xmlElement_arc1);
Arc arc2_fromXml = getAsArc(xmlElement_arc2);

PNode node1 = PPath.createRoundRectangle(x_fromXml , y_fromXml , width_fromXml , heigth_fromXml , arc1_fromXml , arc2_fromXml );



使用getAsInt()和getAsArc()解析提供的XML元素并返回适当的数据类型?

如果那不是解决方案,请再次说明您的问题,因为您说可以解析并打印XML中的内容,但不能将其放入提供的代码行中.

如果您不知道如何将String(从XML返回)解析为int等,那么它就来了:



With getAsInt() and getAsArc() parses the provided XML-Element and returns the appropriate data type?

If that is not the Solution, then please state your problem again, since you said that you could parse and print the stuff from the XML, but not put it into your provided line of code.

If you don''t know how to parse a String (returned from the XML) into int etc, then here it comes:

Integer myInt = Integer.parse(stringFromXml);



与Dounble,Float或Long类似.此外,如果解析失败,您可能想捕获NumberFormatException.

干杯,阿恩特



Similar for Dounble, Float or Long. Additionally you might want to catch a NumberFormatException in case the Parsing failed.

Cheers, Arndt


这篇关于XML和Java帮助的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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