2007-09-30
JAXB data binding .boolean type
关键字: JAXB XML
i try to use JAXB to binding the XML data to class. and i met some question that really boring.
the key of the question is : when i use the JAXB to get the xml data. the boolean data which in the in the created object is not correct.
here are my dtd file and xml file.
conditions.dtd :
<!ELEMENT conditions (condition*)>
<!ELEMENT condition (entrytype,type,leastNum,auto,unit,originate,last) >
<!ELEMENT entrytype (#PCDATA)>
<!ELEMENT type (#PCDATA)>
<!ELEMENT originate (#PCDATA)>
<!ELEMENT auto (#PCDATA)>
<!ELEMENT unit (#PCDATA)>
<!ELEMENT leastNum (#PCDATA)>
<!ELEMENT last (#PCDATA)>
<!ATTLIST condition isInfoComplete CDATA "true">
conditions.xml :
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE conditions SYSTEM "conditions.dtd"><conditions>
<condition isInfoComplete="true">
<entrytype>blogentry</entrytype>
<type>apply</type>
<leastNum>3</leastNum>
<auto>true</auto>
<unit>week</unit>
<originate>true</originate>
<last>y</last>
</condition>
<condition isInfoComplete="true'">
<entrytype>blogentry</entrytype>
<type>keep</type>
<leastNum>20</leastNum>
<auto>true</auto>
<unit>month</unit>
<originate>false</originate>
<last>m</last>
</condition>
</conditions>
the class file is generated by the JAXB tool. and follow is my test class. notice the pakage path.
CollectionTest.java:public class ConditionTest {
public static void main(String[] args) {
try {
JAXBContext jc = JAXBContext.newInstance("test.xmlparse");
Unmarshaller u = jc.createUnmarshaller();
Conditions conds = (Conditions) u.unmarshal(new FileInputStream(
new File("conditions.xml")));
List cond=conds.getCondition();
int size = cond.size();
for(int i=0;i<size;i++) {
Condition con=(Condition) cond.get(i);
print(con);
}
System.out.println(conds.getCondition().size());
} catch (Exception ex) {
System.out.println(ex.getMessage());
System.out.println(ex.getCause());
}
}
private static void print(Condition con) {
System.out.println("auto:"+con.getAuto()+"\nlast:"+con.getLast()+"\nentrytype:"+con.getEntrytype()+"\ntype:"+
con.getType()+"\nunit:"+con.getUnit()+"\nisInfoComplete:"+con.getisInfoComplete()+
"\nleastNum:"+con.getLeastNum()+"\noriginate:"+con.getOriginate()+"\n\n\n");
}
}
here is my result:
auto:true
last:y
entrytype:blogentry
type:apply
unit:week
isInfoComplete:false
leastNum:3
originate:true
auto:true
last:m
entrytype:blogentry
type:keep
unit:month
isInfoComplete:false
leastNum:20
originate:false
-----------------------------------------------------------------------
i tracked the program. the value of isInfoComplete have get when the object created.but the value is not correct.:twisted:
the key of the question is : when i use the JAXB to get the xml data. the boolean data which in the in the created object is not correct.
here are my dtd file and xml file.
conditions.dtd :
<!ELEMENT conditions (condition*)>
<!ELEMENT condition (entrytype,type,leastNum,auto,unit,originate,last) >
<!ELEMENT entrytype (#PCDATA)>
<!ELEMENT type (#PCDATA)>
<!ELEMENT originate (#PCDATA)>
<!ELEMENT auto (#PCDATA)>
<!ELEMENT unit (#PCDATA)>
<!ELEMENT leastNum (#PCDATA)>
<!ELEMENT last (#PCDATA)>
<!ATTLIST condition isInfoComplete CDATA "true">
conditions.xml :
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE conditions SYSTEM "conditions.dtd"><conditions>
<condition isInfoComplete="true">
<entrytype>blogentry</entrytype>
<type>apply</type>
<leastNum>3</leastNum>
<auto>true</auto>
<unit>week</unit>
<originate>true</originate>
<last>y</last>
</condition>
<condition isInfoComplete="true'">
<entrytype>blogentry</entrytype>
<type>keep</type>
<leastNum>20</leastNum>
<auto>true</auto>
<unit>month</unit>
<originate>false</originate>
<last>m</last>
</condition>
</conditions>
the class file is generated by the JAXB tool. and follow is my test class. notice the pakage path.
CollectionTest.java:public class ConditionTest {
public static void main(String[] args) {
try {
JAXBContext jc = JAXBContext.newInstance("test.xmlparse");
Unmarshaller u = jc.createUnmarshaller();
Conditions conds = (Conditions) u.unmarshal(new FileInputStream(
new File("conditions.xml")));
List cond=conds.getCondition();
int size = cond.size();
for(int i=0;i<size;i++) {
Condition con=(Condition) cond.get(i);
print(con);
}
System.out.println(conds.getCondition().size());
} catch (Exception ex) {
System.out.println(ex.getMessage());
System.out.println(ex.getCause());
}
}
private static void print(Condition con) {
System.out.println("auto:"+con.getAuto()+"\nlast:"+con.getLast()+"\nentrytype:"+con.getEntrytype()+"\ntype:"+
con.getType()+"\nunit:"+con.getUnit()+"\nisInfoComplete:"+con.getisInfoComplete()+
"\nleastNum:"+con.getLeastNum()+"\noriginate:"+con.getOriginate()+"\n\n\n");
}
}
here is my result:
auto:true
last:y
entrytype:blogentry
type:apply
unit:week
isInfoComplete:false
leastNum:3
originate:true
auto:true
last:m
entrytype:blogentry
type:keep
unit:month
isInfoComplete:false
leastNum:20
originate:false
-----------------------------------------------------------------------
i tracked the program. the value of isInfoComplete have get when the object created.but the value is not correct.:twisted:
评论
jonson
2007-10-16
the reason i have find .it is caused by the dtd file is not work when the condition.java file to been created .
- 浏览: 2853 次
- 性别:

- 来自: 杭州

- 详细资料
搜索本博客
我的相册
大竹海04
共 2 张
共 2 张
最近加入圈子
最新评论
-
js 函数的定义、运行 顺 ...
niuyuewanzi 写道jonson 写道afcn0 写道大哥你不会指的是这 ...
-- by cyan19851224 -
题库贴
1。100的阶乘的程序: /* * To change this t ...
-- by jonson -
题库贴
3.SELECT PCLASS, PSCORE from (select PCL ...
-- by firefox_1983 -
题库贴
难道大家都是超级高手!!都不需要练手?还是觉得题目太简单!其实在工作之余 做做小 ...
-- by jonson -
丑陋的继承
jonson 写道bluemeteor 写道AbstractTopicPO |_ ...
-- by 抛出异常的爱






评论排行榜