java读取xml指定标签的内容
1. package com.fenuang.sec;
2.
3. import java.io.File;
4. import java.util.ArrayList;
5. import java.util.Collection;
6. import java.util.HashMap;
7. import java.util.Iterator;
8. import java.util.List;
9.
10. import javax.print.Doc;
11.
12. import org.dom4j.Document;
13. import org.dom4j.Element;
14. import org.dom4j.io.SAXReader;
15.
16. import com.st;
17.
18. public class trueuse {
19.
20. public static void main(String[] args) {
21. // TODO Auto-generated method stub
22.
23. try {
24. // 实例化⼀个类⽤于添加xml⽂件
25. List<contest> list = new ArrayList<>();
26.
27. SAXReader reader = new SAXReader();
28. Document doc = ad(new File("./l"));
29. // 读取指定标签
30. Iterator<Element> eleit = RootElement().elementIterator("user");
31.
32. ArrayList newlist = new ArrayList(); // 创建新集合
33. while (eleit.hasNext()) {
34.
35. Element ele = ();
36. contest con = new contest();
37. con.setId(ele.attributeValue("id"));
38. con.setName(ele.elementText("name"));
39. con.setEmail(ele.elementText("email"));
40. con.setQq(ele.elementText("qq"));
41. con.setWife(ele.elementText("wife"));
42. list.add(con);
43.
44. Iterator it = list.iterator(); // 通过⽼集合获取迭代器
45. while (it.hasNext()) { // 遍历⽼集合
46. Object obj = it.next(); // 得到⽼集合的元素
47. if (!ains(obj)) {
48. newlist.add(obj);
49. }
50. }
51.
52. }
53.
53.
54. System.out.(0));
55. // System.out.println(" =======");
56. System.out.(1));
57. // System.out.println(" =======");
58. System.out.(2));
59.
60. } catch (
61.
62. Exception e) {
63. e.printStackTrace();
64. }
65.
66. }
67.
68. }
下⾯是⽤于存储元素的类
[java]
1. package com.fenuang.demo4j;
2.
3. Messaging.SyncScopeHelper;
4.
5. public class contest {
6. private String id;
xml标签大全7. private String name;
8. private String email;
9. private String qq;
10. private String wife;
11.
12. /**
13. * @param id
14. * @param name
15. * @param email
16. * @param qq
17. * @param wife
18. */
19. public contest(String id, String name, String email, String qq, String wife) {
20. super();
21. this.id = id;
22. this.name = name;
23. ail = email;
24. this.qq = qq;
25. this.wife = wife;
26. }
27.
28. /**
29. * @param id
30. * @param name
31. * @param email
32. * @param qq
32. * @param qq
33. * @param wife
34. */
35. public contest() {
36.
37. }
38.
39. public String getId() {
40. return id;
41. }
42.
43. public void setId(String id) {
44. this.id = id;
45. }
46.
47. public String getName() {
48. return name;
49. }
50.
51. public void setName(String name) {
52. this.name = name;
53. }
54.
55. public String getEmail() {
56. return email;
57. }
58.
59. public void setEmail(String email) {
60. ail = email;
61. }
62.
63. public String getQq() {
64. return qq;
65. }
66.
67. public void setQq(String qq) {
68. this.qq = qq;
69. }
70.
71. public String getWife() {
72. return wife;
73. }
74.
75. public void setWife(String wife) {
76. this.wife = wife;
77. }
78.
79. @Override
80. public String toString() {
81. // TODO Auto-generated method stub
82. ail + "--" + this.id + "--" + this.name + "--" + this.qq + "--" + this.wife+" ";
83.
84.
85. }
86.
87. }
87. }
下⾯是要被读取的xml⽂件
[html]
1. <?xml version="1.0"encoding="utf-8"?>
2.
3. <contestList>
4. <user id="001">
5. <name>王五</name>
6. <email>2222222</email>
7. <qq>4fenuang</qq>
8. <wife>6fenuang</wife>
9. </user>
10.
11. <user id="002">
12. <name>李四</name>
13. <email>4444444</email>
14. <qq>4fenuang</qq>
15. <wife>6fenuang</wife>
16. </user>
17.
18.
19. <user id="003">
20. <name>张三</name>
21. <email>2fenuang</email>
22. <qq>333333</qq>
23. <wife>6fenuang</wife>
24. </user>
25.
26.
27.
28. </contestList>
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论