- 浏览: 95072 次
- 性别:
- 来自: 厦门
文章分类
最新评论
-
石头茨:
你好,除了jocky能够比较好的混淆编译器之外,还有没有其他的 ...
java各种代码混淆方法的优缺点 -
java_ZJian:
谢谢
slf4j配置实例 -
f839903061:
楼主有具体步骤吗?
java代码混淆--jocky -
huiqiCHEN1989:
学习,谢谢分享。
java程序员白盒单元测试必备技能 -- Junit4 -
MagicPocket:
哥也是 ios 开发,最近在接触 android ,共勉。。。 ...
开始学习android努力~~
转换Xml与java object,一般使用JDom或SAX。但这样手动mapping,代码实在冗长乏味。目前已有jaxb,castor, xstream等类库提供自动映射,如Xfire的webservice就是基于这些类库进行xml解组.
1.已定义xml模板文件
Xml代码
1.<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
2.<job-config>
3. <purge-interval>3</purge-interval>
4. <db-info>
5. <dbtype>Oracle</dbtype>
6. <url>oracle:thin:localhost:orcl</url>
7. <username>admin</username>
8. <password>admin</password>
9. </db-info>
10. <job-info>
11. <frequency>
12. <interval-type>WEEKLY</interval-type>
13. <interval>10</interval>
14. <start-time>09:00</start-time>
15. </frequency>
16. <objects-component>
17. <object>
18. <object-name>RUNTIME</object-name>
19. <retention>10</retention>
20. </object>
21. <object>
22. <object-name>BUILDTIME</object-name>
23. <retention>20</retention>
24. </object>
25. </objects-component>
26. <objects-specific>
27. <object>
28. <object-name>GOOD</object-name>
29. <retention>10</retention>
30. </object>
31. <object>
32. <object-name>BUILDTIME</object-name>
33. <retention>20</retention>
34. </object>
35. </objects-specific>
36. </job-info>
37.</job-config>
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<job-config>
<purge-interval>3</purge-interval>
<db-info>
<dbtype>Oracle</dbtype>
<url>oracle:thin:localhost:orcl</url>
<username>admin</username>
<password>admin</password>
</db-info>
<job-info>
<frequency>
<interval-type>WEEKLY</interval-type>
<interval>10</interval>
<start-time>09:00</start-time>
</frequency>
<objects-component>
<object>
<object-name>RUNTIME</object-name>
<retention>10</retention>
</object>
<object>
<object-name>BUILDTIME</object-name>
<retention>20</retention>
</object>
</objects-component>
<objects-specific>
<object>
<object-name>GOOD</object-name>
<retention>10</retention>
</object>
<object>
<object-name>BUILDTIME</object-name>
<retention>20</retention>
</object>
</objects-specific>
</job-info>
</job-config>
2.产生xsd文件
手动编写xsd当然很不方便,需要你学习太多xsd的语法细节,在我看来使用时可以参考即可。有一些工具可以自动产生xsd文件。譬如MS Visustido提供了xsd.exe,也有其他开源类库(trang)。
这份代码是使用xsd基于xml来产生xsd,
Shell代码
1."C:\Program Files\Microsoft Visual Studio 8\SDK\v2.0\Bin\xsd.exe" E:\temp\JobsConfig.xml /outputdir:/outputdir:e:/temp
"C:\Program Files\Microsoft Visual Studio 8\SDK\v2.0\Bin\xsd.exe" E:\temp\JobsConfig.xml /outputdir:/outputdir:e:/temp
Xml代码
1.<?xml version="1.0" encoding="utf-8"?>
2.<xs:schema id="NewDataSet" xmlns="" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
3. . . .
4. <xs:element name="job-config">
5. <xs:complexType>
6. <xs:sequence>
7. <xs:element name="purge-interval" type="xs:string" minOccurs="0" />
8. <xs:element name="db-info" minOccurs="0" maxOccurs="1">
9. <xs:complexType>
10. <xs:sequence>
11. <xs:element name="dbtype" type="xs:string" minOccurs="0" />
12. <xs:element name="url" type="xs:string" minOccurs="0" />
13. <xs:element name="username" type="xs:string" minOccurs="0" />
14. <xs:element name="password" type="xs:string" minOccurs="0" />
15. </xs:sequence>
16. </xs:complexType>
17. </xs:element>
18.<!-- maxOccurs根据需要从unbounded调整为1,则产生的对象从默认的list转为object -->
19. <xs:element name="job-info" minOccurs="0" maxOccurs="1">
20. <xs:complexType>
21. <xs:sequence>
22. <xs:element name="frequency" minOccurs="0" maxOccurs="1">
23. <xs:complexType>
24. <xs:sequence>
25. <xs:element name="interval-type" type="xs:string" minOccurs="0" />
26. <xs:element name="interval" type="xs:string" minOccurs="0" />
27. <xs:element name="start-time" type="xs:string" minOccurs="0" />
28. </xs:sequence>
29. </xs:complexType>
30. </xs:element>
31. <xs:element name="objects-component" minOccurs="0" maxOccurs="1">
32. <xs:complexType>
33. <xs:sequence>
34. <xs:element ref="object" minOccurs="0" maxOccurs="unbounded" />
35. </xs:sequence>
36. </xs:complexType>
37. </xs:element>
38. <xs:element name="objects-specific" minOccurs="0" maxOccurs="1">
39. <xs:complexType>
40. <xs:sequence>
41. <xs:element ref="object" minOccurs="0" maxOccurs="unbounded" />
42. </xs:sequence>
43. </xs:complexType>
44. </xs:element>
45. </xs:sequence>
46. </xs:complexType>
47. </xs:element>
48. </xs:sequence>
49. </xs:complexType>
50. </xs:element>
51. . . .
52. </xs:element>
<?xml version="1.0" encoding="utf-8"?>
<xs:schema id="NewDataSet" xmlns="" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
. . .
<xs:element name="job-config">
<xs:complexType>
<xs:sequence>
<xs:element name="purge-interval" type="xs:string" minOccurs="0" />
<xs:element name="db-info" minOccurs="0" maxOccurs="1">
<xs:complexType>
<xs:sequence>
<xs:element name="dbtype" type="xs:string" minOccurs="0" />
<xs:element name="url" type="xs:string" minOccurs="0" />
<xs:element name="username" type="xs:string" minOccurs="0" />
<xs:element name="password" type="xs:string" minOccurs="0" />
</xs:sequence>
</xs:complexType>
</xs:element>
<!-- maxOccurs根据需要从unbounded调整为1,则产生的对象从默认的list转为object -->
<xs:element name="job-info" minOccurs="0" maxOccurs="1">
<xs:complexType>
<xs:sequence>
<xs:element name="frequency" minOccurs="0" maxOccurs="1">
<xs:complexType>
<xs:sequence>
<xs:element name="interval-type" type="xs:string" minOccurs="0" />
<xs:element name="interval" type="xs:string" minOccurs="0" />
<xs:element name="start-time" type="xs:string" minOccurs="0" />
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="objects-component" minOccurs="0" maxOccurs="1">
<xs:complexType>
<xs:sequence>
<xs:element ref="object" minOccurs="0" maxOccurs="unbounded" />
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="objects-specific" minOccurs="0" maxOccurs="1">
<xs:complexType>
<xs:sequence>
<xs:element ref="object" minOccurs="0" maxOccurs="unbounded" />
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
. . .
</xs:element>
3.运行xjc命令由xsd生成java对象
可将JAXB的xjc.bat配置为Eclipse的external tool.对象如下,
4.依据xsd格式从xml中读取数据,unmarshal
Java代码
1.File xmlDocument = new File("res/JobsConfig.xml");
2.JAXBContext jaxbContext = JAXBContext.newInstance("generated");
3. Unmarshaller unMarshaller = jaxbContext.createUnmarshaller();
4. SchemaFactory schemaFactory = SchemaFactory.newInstance("http://www.w3.org/2001/XMLSchema");
5. Schema schema = schemaFactory.newSchema(new File("res/JobsConfig.xsd"));
6. unMarshaller.setSchema(schema);
7.
8. JobConfig config = (JobConfig) unMarshaller.unmarshal(xmlDocument);
9.
10. System.out.println("purge-interval: " + config.getPurgeInterval());
11.
12. DbInfo dbInfo = config.getDbInfo();
13.
14. System.out.println("DB type:" + dbInfo.getDbtype());
15. System.out.println("Url type:" + dbInfo.getUrl().trim());
16. System.out.println("Username type:" + dbInfo.getUsername());
17. System.out.println("Password type:" + dbInfo.getPassword());
18.
19. JobInfo jobInfo = config.getJobInfo();
20. System.out.println("Frequency:" + jobInfo.getFrequency().getStartTime());
21.
22. }
23. catch (JAXBException e)
24. {
25. System.out.println(e.toString());
26. }
27. catch (SAXException e)
28. {
29. System.out.println(e.toString());
30. }
File xmlDocument = new File("res/JobsConfig.xml");
JAXBContext jaxbContext = JAXBContext.newInstance("generated");
Unmarshaller unMarshaller = jaxbContext.createUnmarshaller();
SchemaFactory schemaFactory = SchemaFactory.newInstance("http://www.w3.org/2001/XMLSchema");
Schema schema = schemaFactory.newSchema(new File("res/JobsConfig.xsd"));
unMarshaller.setSchema(schema);
JobConfig config = (JobConfig) unMarshaller.unmarshal(xmlDocument);
System.out.println("purge-interval: " + config.getPurgeInterval());
DbInfo dbInfo = config.getDbInfo();
System.out.println("DB type:" + dbInfo.getDbtype());
System.out.println("Url type:" + dbInfo.getUrl().trim());
System.out.println("Username type:" + dbInfo.getUsername());
System.out.println("Password type:" + dbInfo.getPassword());
JobInfo jobInfo = config.getJobInfo();
System.out.println("Frequency:" + jobInfo.getFrequency().getStartTime());
}
catch (JAXBException e)
{
System.out.println(e.toString());
}
catch (SAXException e)
{
System.out.println(e.toString());
}
5.构造java对象,产生xml数据,marshal
Java代码
1. File xmlDocument = new File("jobConfig.xml");
2. JAXBContext jaxbContext = JAXBContext.newInstance("generated");
3. Marshaller marshaller = jaxbContext.createMarshaller();
4. marshaller.setProperty(
5. Marshaller.JAXB_FORMATTED_OUTPUT,
6. true);
7. ObjectFactory factory = new ObjectFactory();
8.
9. JobConfig config = factory.createJobConfig();
10. config.setPurgeInterval("3");
11. // construct db info
12. DbInfo dbInfo = factory.createJobConfigDbInfo();
13. dbInfo.setDbtype("Oracle");
14. dbInfo.setUrl("oracle:thin:localhost:orcl");
15. dbInfo.setUsername("admin");
16. dbInfo.setPassword("admin");
17. config.setDbInfo(dbInfo);
18.
19. // construct job info
20. JobInfo jobInfo = factory.createJobConfigJobInfo();
21. // job info - frequency
22. Frequency frequency = factory.createJobConfigJobInfoFrequency();
23. frequency.setInterval("10");
24. frequency.setIntervalType("WEEKLY");
25. frequency.setStartTime("09:00");
26. jobInfo.setFrequency(frequency);
27. // job info - ObjectsComponent
28. ObjectsComponent objectComponet = factory.createJobConfigJobInfoObjectsComponent();
29. Object runtimeObj = factory.createObject();
30. runtimeObj.setObjectName("RUNTIME");
31. runtimeObj.setRetention("10");
32. objectComponet.getObject().add(
33. runtimeObj);
34. Object buildtimeObj = factory.createObject();
35. buildtimeObj.setObjectName("BUILDTIME");
36. buildtimeObj.setRetention("20");
37. objectComponet.getObject().add(
38. buildtimeObj);
39. jobInfo.setObjectsComponent(objectComponet);
40. // job info - ObjectsSpecific
41. ObjectsSpecific objectSpecfic = factory.createJobConfigJobInfoObjectsSpecific();
42. Object tObj1 = factory.createObject();
43. tObj1.setObjectName("GOOD");
44. tObj1.setRetention("10");
45. objectSpecfic.getObject().add(
46. tObj1);
47. Object tObj2 = factory.createObject();
48. tObj2.setObjectName("FAT");
49. tObj2.setRetention("20");
50. objectSpecfic.getObject().add(
51. buildtimeObj);
52. jobInfo.setObjectsSpecific(objectSpecfic);
53.
54. config.setJobInfo(jobInfo);
55.
56. // marshal to file
57. marshaller.marshal(
58. config,
59. new FileOutputStream(xmlDocument));
60. // marshal to console
61. marshaller.marshal(
62. config,
63. System.out);
64.}
65.catch (IOException e)
66.{
67. System.out.println(e.toString());
68.}
69.catch (JAXBException e)
70.{
71. System.out.println(e.toString());
72.}
1.已定义xml模板文件
Xml代码
1.<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
2.<job-config>
3. <purge-interval>3</purge-interval>
4. <db-info>
5. <dbtype>Oracle</dbtype>
6. <url>oracle:thin:localhost:orcl</url>
7. <username>admin</username>
8. <password>admin</password>
9. </db-info>
10. <job-info>
11. <frequency>
12. <interval-type>WEEKLY</interval-type>
13. <interval>10</interval>
14. <start-time>09:00</start-time>
15. </frequency>
16. <objects-component>
17. <object>
18. <object-name>RUNTIME</object-name>
19. <retention>10</retention>
20. </object>
21. <object>
22. <object-name>BUILDTIME</object-name>
23. <retention>20</retention>
24. </object>
25. </objects-component>
26. <objects-specific>
27. <object>
28. <object-name>GOOD</object-name>
29. <retention>10</retention>
30. </object>
31. <object>
32. <object-name>BUILDTIME</object-name>
33. <retention>20</retention>
34. </object>
35. </objects-specific>
36. </job-info>
37.</job-config>
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<job-config>
<purge-interval>3</purge-interval>
<db-info>
<dbtype>Oracle</dbtype>
<url>oracle:thin:localhost:orcl</url>
<username>admin</username>
<password>admin</password>
</db-info>
<job-info>
<frequency>
<interval-type>WEEKLY</interval-type>
<interval>10</interval>
<start-time>09:00</start-time>
</frequency>
<objects-component>
<object>
<object-name>RUNTIME</object-name>
<retention>10</retention>
</object>
<object>
<object-name>BUILDTIME</object-name>
<retention>20</retention>
</object>
</objects-component>
<objects-specific>
<object>
<object-name>GOOD</object-name>
<retention>10</retention>
</object>
<object>
<object-name>BUILDTIME</object-name>
<retention>20</retention>
</object>
</objects-specific>
</job-info>
</job-config>
2.产生xsd文件
手动编写xsd当然很不方便,需要你学习太多xsd的语法细节,在我看来使用时可以参考即可。有一些工具可以自动产生xsd文件。譬如MS Visustido提供了xsd.exe,也有其他开源类库(trang)。
这份代码是使用xsd基于xml来产生xsd,
Shell代码
1."C:\Program Files\Microsoft Visual Studio 8\SDK\v2.0\Bin\xsd.exe" E:\temp\JobsConfig.xml /outputdir:/outputdir:e:/temp
"C:\Program Files\Microsoft Visual Studio 8\SDK\v2.0\Bin\xsd.exe" E:\temp\JobsConfig.xml /outputdir:/outputdir:e:/temp
Xml代码
1.<?xml version="1.0" encoding="utf-8"?>
2.<xs:schema id="NewDataSet" xmlns="" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
3. . . .
4. <xs:element name="job-config">
5. <xs:complexType>
6. <xs:sequence>
7. <xs:element name="purge-interval" type="xs:string" minOccurs="0" />
8. <xs:element name="db-info" minOccurs="0" maxOccurs="1">
9. <xs:complexType>
10. <xs:sequence>
11. <xs:element name="dbtype" type="xs:string" minOccurs="0" />
12. <xs:element name="url" type="xs:string" minOccurs="0" />
13. <xs:element name="username" type="xs:string" minOccurs="0" />
14. <xs:element name="password" type="xs:string" minOccurs="0" />
15. </xs:sequence>
16. </xs:complexType>
17. </xs:element>
18.<!-- maxOccurs根据需要从unbounded调整为1,则产生的对象从默认的list转为object -->
19. <xs:element name="job-info" minOccurs="0" maxOccurs="1">
20. <xs:complexType>
21. <xs:sequence>
22. <xs:element name="frequency" minOccurs="0" maxOccurs="1">
23. <xs:complexType>
24. <xs:sequence>
25. <xs:element name="interval-type" type="xs:string" minOccurs="0" />
26. <xs:element name="interval" type="xs:string" minOccurs="0" />
27. <xs:element name="start-time" type="xs:string" minOccurs="0" />
28. </xs:sequence>
29. </xs:complexType>
30. </xs:element>
31. <xs:element name="objects-component" minOccurs="0" maxOccurs="1">
32. <xs:complexType>
33. <xs:sequence>
34. <xs:element ref="object" minOccurs="0" maxOccurs="unbounded" />
35. </xs:sequence>
36. </xs:complexType>
37. </xs:element>
38. <xs:element name="objects-specific" minOccurs="0" maxOccurs="1">
39. <xs:complexType>
40. <xs:sequence>
41. <xs:element ref="object" minOccurs="0" maxOccurs="unbounded" />
42. </xs:sequence>
43. </xs:complexType>
44. </xs:element>
45. </xs:sequence>
46. </xs:complexType>
47. </xs:element>
48. </xs:sequence>
49. </xs:complexType>
50. </xs:element>
51. . . .
52. </xs:element>
<?xml version="1.0" encoding="utf-8"?>
<xs:schema id="NewDataSet" xmlns="" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
. . .
<xs:element name="job-config">
<xs:complexType>
<xs:sequence>
<xs:element name="purge-interval" type="xs:string" minOccurs="0" />
<xs:element name="db-info" minOccurs="0" maxOccurs="1">
<xs:complexType>
<xs:sequence>
<xs:element name="dbtype" type="xs:string" minOccurs="0" />
<xs:element name="url" type="xs:string" minOccurs="0" />
<xs:element name="username" type="xs:string" minOccurs="0" />
<xs:element name="password" type="xs:string" minOccurs="0" />
</xs:sequence>
</xs:complexType>
</xs:element>
<!-- maxOccurs根据需要从unbounded调整为1,则产生的对象从默认的list转为object -->
<xs:element name="job-info" minOccurs="0" maxOccurs="1">
<xs:complexType>
<xs:sequence>
<xs:element name="frequency" minOccurs="0" maxOccurs="1">
<xs:complexType>
<xs:sequence>
<xs:element name="interval-type" type="xs:string" minOccurs="0" />
<xs:element name="interval" type="xs:string" minOccurs="0" />
<xs:element name="start-time" type="xs:string" minOccurs="0" />
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="objects-component" minOccurs="0" maxOccurs="1">
<xs:complexType>
<xs:sequence>
<xs:element ref="object" minOccurs="0" maxOccurs="unbounded" />
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="objects-specific" minOccurs="0" maxOccurs="1">
<xs:complexType>
<xs:sequence>
<xs:element ref="object" minOccurs="0" maxOccurs="unbounded" />
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
. . .
</xs:element>
3.运行xjc命令由xsd生成java对象
可将JAXB的xjc.bat配置为Eclipse的external tool.对象如下,
4.依据xsd格式从xml中读取数据,unmarshal
Java代码
1.File xmlDocument = new File("res/JobsConfig.xml");
2.JAXBContext jaxbContext = JAXBContext.newInstance("generated");
3. Unmarshaller unMarshaller = jaxbContext.createUnmarshaller();
4. SchemaFactory schemaFactory = SchemaFactory.newInstance("http://www.w3.org/2001/XMLSchema");
5. Schema schema = schemaFactory.newSchema(new File("res/JobsConfig.xsd"));
6. unMarshaller.setSchema(schema);
7.
8. JobConfig config = (JobConfig) unMarshaller.unmarshal(xmlDocument);
9.
10. System.out.println("purge-interval: " + config.getPurgeInterval());
11.
12. DbInfo dbInfo = config.getDbInfo();
13.
14. System.out.println("DB type:" + dbInfo.getDbtype());
15. System.out.println("Url type:" + dbInfo.getUrl().trim());
16. System.out.println("Username type:" + dbInfo.getUsername());
17. System.out.println("Password type:" + dbInfo.getPassword());
18.
19. JobInfo jobInfo = config.getJobInfo();
20. System.out.println("Frequency:" + jobInfo.getFrequency().getStartTime());
21.
22. }
23. catch (JAXBException e)
24. {
25. System.out.println(e.toString());
26. }
27. catch (SAXException e)
28. {
29. System.out.println(e.toString());
30. }
File xmlDocument = new File("res/JobsConfig.xml");
JAXBContext jaxbContext = JAXBContext.newInstance("generated");
Unmarshaller unMarshaller = jaxbContext.createUnmarshaller();
SchemaFactory schemaFactory = SchemaFactory.newInstance("http://www.w3.org/2001/XMLSchema");
Schema schema = schemaFactory.newSchema(new File("res/JobsConfig.xsd"));
unMarshaller.setSchema(schema);
JobConfig config = (JobConfig) unMarshaller.unmarshal(xmlDocument);
System.out.println("purge-interval: " + config.getPurgeInterval());
DbInfo dbInfo = config.getDbInfo();
System.out.println("DB type:" + dbInfo.getDbtype());
System.out.println("Url type:" + dbInfo.getUrl().trim());
System.out.println("Username type:" + dbInfo.getUsername());
System.out.println("Password type:" + dbInfo.getPassword());
JobInfo jobInfo = config.getJobInfo();
System.out.println("Frequency:" + jobInfo.getFrequency().getStartTime());
}
catch (JAXBException e)
{
System.out.println(e.toString());
}
catch (SAXException e)
{
System.out.println(e.toString());
}
5.构造java对象,产生xml数据,marshal
Java代码
1. File xmlDocument = new File("jobConfig.xml");
2. JAXBContext jaxbContext = JAXBContext.newInstance("generated");
3. Marshaller marshaller = jaxbContext.createMarshaller();
4. marshaller.setProperty(
5. Marshaller.JAXB_FORMATTED_OUTPUT,
6. true);
7. ObjectFactory factory = new ObjectFactory();
8.
9. JobConfig config = factory.createJobConfig();
10. config.setPurgeInterval("3");
11. // construct db info
12. DbInfo dbInfo = factory.createJobConfigDbInfo();
13. dbInfo.setDbtype("Oracle");
14. dbInfo.setUrl("oracle:thin:localhost:orcl");
15. dbInfo.setUsername("admin");
16. dbInfo.setPassword("admin");
17. config.setDbInfo(dbInfo);
18.
19. // construct job info
20. JobInfo jobInfo = factory.createJobConfigJobInfo();
21. // job info - frequency
22. Frequency frequency = factory.createJobConfigJobInfoFrequency();
23. frequency.setInterval("10");
24. frequency.setIntervalType("WEEKLY");
25. frequency.setStartTime("09:00");
26. jobInfo.setFrequency(frequency);
27. // job info - ObjectsComponent
28. ObjectsComponent objectComponet = factory.createJobConfigJobInfoObjectsComponent();
29. Object runtimeObj = factory.createObject();
30. runtimeObj.setObjectName("RUNTIME");
31. runtimeObj.setRetention("10");
32. objectComponet.getObject().add(
33. runtimeObj);
34. Object buildtimeObj = factory.createObject();
35. buildtimeObj.setObjectName("BUILDTIME");
36. buildtimeObj.setRetention("20");
37. objectComponet.getObject().add(
38. buildtimeObj);
39. jobInfo.setObjectsComponent(objectComponet);
40. // job info - ObjectsSpecific
41. ObjectsSpecific objectSpecfic = factory.createJobConfigJobInfoObjectsSpecific();
42. Object tObj1 = factory.createObject();
43. tObj1.setObjectName("GOOD");
44. tObj1.setRetention("10");
45. objectSpecfic.getObject().add(
46. tObj1);
47. Object tObj2 = factory.createObject();
48. tObj2.setObjectName("FAT");
49. tObj2.setRetention("20");
50. objectSpecfic.getObject().add(
51. buildtimeObj);
52. jobInfo.setObjectsSpecific(objectSpecfic);
53.
54. config.setJobInfo(jobInfo);
55.
56. // marshal to file
57. marshaller.marshal(
58. config,
59. new FileOutputStream(xmlDocument));
60. // marshal to console
61. marshaller.marshal(
62. config,
63. System.out);
64.}
65.catch (IOException e)
66.{
67. System.out.println(e.toString());
68.}
69.catch (JAXBException e)
70.{
71. System.out.println(e.toString());
72.}
相关推荐
### JAXB使用步骤 1. **创建Java模型类**:定义要序列化的Java类,使用注解如`@XmlRootElement`,`@XmlElement`等来标注字段。 2. **设置JAXB上下文**:使用`JAXBContext`来获取Java类型和XML之间的映射信息。这...
1. **编译时绑定**:在开发阶段,JAXB使用`xjc`工具将XML Schema(XSD)文件转换为一组Java类,这些类包含了对XML结构的表示。这个过程也被称为“逆向工程”。 2. **运行时转换**:在程序运行时,JAXB提供了一组API...
如何更好地jaxb使用如何更好地jaxb使用如何更好地jaxb使用如何更好地jaxb使用
在本示例中,"JAXB使用Socket流进行双向通信源码"结合了这两个概念,创建了一个基于Java的客户端(Client_Socket)应用,实现了XML数据在客户端和服务端之间的双向传输。 首先,我们要理解JAXB的工作原理。JAXB允许...
### JAXB使用场景 - 数据交换:在分布式系统中,服务间通信常通过XML格式的数据交换,JAXB提供了便捷的序列化和反序列化手段。 - 配置文件解析:XML常被用作应用程序的配置文件,JAXB能帮助解析和操作这些配置。 - ...
1. 什么是JAXB? Java Architecture for XML Binding (JAXB) 是一个业界的标准,是一项可以根据XML
2. **实例化(Unmarshalling)**:当需要将XML数据转换为Java对象时,JAXB使用Unmarshaller接口解析XML文档,生成对应的Java对象。这个过程是将XML数据"反序列化"为可操作的Java对象。 3. **序列化(Marshalling)*...
- **软件许可**:明确了 JAXB 使用时的许可条款,通常遵循开放源代码许可证。 - **示例应用**:包含多个示例应用程序,演示如何使用 JAXB 进行 XML 数据绑定。 - **运行时绑定框架**:这部分详细介绍了如何在运行时...
### JAXB使用示例 以下是一个简单的JAXB解析生成XML的例子: ```java import javax.xml.bind.JAXBContext; import javax.xml.bind.Marshaller; import javax.xml.bind.Unmarshaller; public class JaxbExample { ...
然后,JAXB使用这些类生成XML Schema(XSD),或者反过来,基于XML Schema生成Java类。这个过程叫做编译或绑定。这种方式使得Java应用程序能够轻松地与XML数据交互,而无需手动解析XML。 **HelloWorld示例** 是学习...
JAXB使用Java注解来指定Java类和XML元素之间的映射关系。例如,我们可以使用`@XmlRootElement`、`@XmlElement`、`@XmlAttribute`等注解来定义Java类的结构。 ```java import javax.xml.bind.annotation....
JAXB使用注解或绑定文件来定义对象和XML之间的映射。例如,当我们有一个`Plan`类,我们可以通过JAXB将其序列化为`plan.xml`文件,反之亦然。 ```java @XmlRootElement(name = "plan") public class Plan { private...
在给定的例子"JAXBDemo"中,我们可以预期它包含了一个简单的JAXB使用示例,帮助我们理解这一过程。 首先,让我们了解一下JAXB的工作原理。JAXB的核心思想是通过元数据(如注解或XML Schema)将Java类与XML结构关联...
**JAXB(Java Architecture for XML ...总结来说,JAXB是一种强大的Java XML处理技术,而XMLSPY作为一个全面的XML工具,是开发和调试XML相关项目的好帮手,尤其在配合JAXB使用时,可以大大提高开发效率和代码质量。
"jaxb使用文档.doc"应该是详细介绍了JAXB的使用方法,包括基本操作、高级特性以及可能遇到的问题和解决方案。文档可能还涵盖了错误处理、性能优化、XML Schema的处理等内容。 总的来说,这份"JAXB资料集.rar"是一份...
JAXB使用`@XmlRootElement`和`@XmlElement`等注解来识别哪些类和字段应该映射到XML元素。默认情况下,JAXB会自动为这些元素和属性分配命名空间前缀。为了自定义这些前缀,我们需要使用`@XmlSchema`、`@...
JAXB使用注解(Annotations)来定义这些类如何映射到XML结构。常见的注解包括: 1. `@XmlRootElement`:标记一个类作为XML文档的根元素。 2. `@XmlElement`:将类的字段映射为XML元素。 3. `@XmlAttribute`:将类的...
JAXB 使用 javax.xml.bind.annotation 包下的 Java 标注,例如: * @XmlRootElement:标注 Java 类为 XML 根元素 * @XmlElement:标注 Java 属性为 XML 元素 * @XmlAttribute:标注 Java 属性为 XML 属性 六、JAXB...
jaxb开发使用文档 JAXB(Java Architecture for XML Binding)是一种java标准库,用于处理XML与java对象的映射。jaxb技术在javaSE6.0中已经包含了jaxb定义的所有API,在处理XML文档时,使用jaxb要比使用JAXP(DOM、...