Skip to content

Commit e6250f2

Browse files
committed
实现 XmlArray
1 parent 9293fd9 commit e6250f2

4 files changed

Lines changed: 75 additions & 22 deletions

File tree

okhttps-xml/src/main/java/com/ejlchina/okhttps/xml/XmlArray.java

Lines changed: 40 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,65 +2,102 @@
22

33
import com.ejlchina.okhttps.Array;
44
import com.ejlchina.okhttps.Mapper;
5+
import org.w3c.dom.Element;
6+
import org.w3c.dom.Node;
57
import org.w3c.dom.NodeList;
68

79
public class XmlArray implements Array {
810

911
private String[] nameKeys;
12+
private String[] valueKeys;
1013
private NodeList list;
1114

12-
public XmlArray(String[] nameKeys, NodeList list) {
15+
public XmlArray(String[] nameKeys, String[] valueKeys, NodeList list) {
1316
this.nameKeys = nameKeys;
17+
this.valueKeys = valueKeys;
1418
this.list = list;
1519
}
1620

1721
@Override
1822
public int size() {
19-
return 0;
23+
return list.getLength();
2024
}
2125

2226
@Override
2327
public boolean isEmpty() {
24-
return false;
28+
return list.getLength() == 0;
2529
}
2630

2731
@Override
2832
public Mapper getMapper(int index) {
33+
if (index < list.getLength()) {
34+
Node node = list.item(index);
35+
if (node instanceof Element) {
36+
return new XmlMapper(nameKeys, valueKeys, (Element) node);
37+
}
38+
}
2939
return null;
3040
}
3141

3242
@Override
3343
public Array getArray(int index) {
44+
if (index < list.getLength()) {
45+
Node node = list.item(index);
46+
return new XmlArray(nameKeys, valueKeys, node.getChildNodes());
47+
}
3448
return null;
3549
}
3650

3751
@Override
3852
public boolean getBool(int index) {
53+
if (index < list.getLength()) {
54+
String value = XmlUtils.value(list.item(index), valueKeys);
55+
return XmlUtils.toBoolean(value);
56+
}
3957
return false;
4058
}
4159

4260
@Override
4361
public int getInt(int index) {
62+
if (index < list.getLength()) {
63+
String value = XmlUtils.value(list.item(index), valueKeys);
64+
return XmlUtils.toInt(value);
65+
}
4466
return 0;
4567
}
4668

4769
@Override
4870
public long getLong(int index) {
71+
if (index < list.getLength()) {
72+
String value = XmlUtils.value(list.item(index), valueKeys);
73+
return XmlUtils.toLong(value);
74+
}
4975
return 0;
5076
}
5177

5278
@Override
5379
public float getFloat(int index) {
80+
if (index < list.getLength()) {
81+
String value = XmlUtils.value(list.item(index), valueKeys);
82+
return XmlUtils.toFloat(value);
83+
}
5484
return 0;
5585
}
5686

5787
@Override
5888
public double getDouble(int index) {
89+
if (index < list.getLength()) {
90+
String value = XmlUtils.value(list.item(index), valueKeys);
91+
return XmlUtils.toDouble(value);
92+
}
5993
return 0;
6094
}
6195

6296
@Override
6397
public String getString(int index) {
98+
if (index < list.getLength()) {
99+
return XmlUtils.value(list.item(index), valueKeys);
100+
}
64101
return null;
65102
}
66103
}

okhttps-xml/src/main/java/com/ejlchina/okhttps/xml/XmlMapper.java

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,12 @@
1111
public class XmlMapper implements Mapper {
1212

1313
private String[] nameKeys;
14+
private String[] valueKeys;
1415
private Element element;
1516

16-
public XmlMapper(String[] nameKeys, Element element) {
17+
public XmlMapper(String[] nameKeys, String[] valueKeys, Element element) {
1718
this.nameKeys = nameKeys;
19+
this.valueKeys = valueKeys;
1820
this.element = element;
1921
}
2022

@@ -35,7 +37,7 @@ public Mapper getMapper(String key) {
3537
NodeList nodes = element.getChildNodes();
3638
Element child = XmlUtils.findElement(nodes, nameKeys, key);
3739
if (child != null) {
38-
return new XmlMapper(nameKeys, child);
40+
return new XmlMapper(nameKeys, valueKeys, child);
3941
}
4042
return null;
4143
}
@@ -45,7 +47,7 @@ public Array getArray(String key) {
4547
NodeList nodes = element.getChildNodes();
4648
List<Element> children = XmlUtils.findElements(nodes, nameKeys, key);
4749
if (children.size() > 1) {
48-
return new XmlArray(nameKeys, new NodeList() {
50+
return new XmlArray(nameKeys, valueKeys, new NodeList() {
4951
@Override
5052
public Node item(int index) {
5153
return children.get(index);
@@ -57,49 +59,49 @@ public int getLength() {
5759
});
5860
} else if (children.size() == 1) {
5961
Element element = children.get(0);
60-
return new XmlArray(nameKeys, element.getChildNodes());
62+
return new XmlArray(nameKeys, valueKeys, element.getChildNodes());
6163
}
6264
return null;
6365
}
6466

6567
@Override
6668
public boolean getBool(String key) {
67-
String value = XmlUtils.getNodeValue(element, nameKeys, key);
69+
String value = XmlUtils.getNodeValue(element, nameKeys, valueKeys, key);
6870
return XmlUtils.toBoolean(value);
6971
}
7072

7173
@Override
7274
public int getInt(String key) {
73-
String value = XmlUtils.getNodeValue(element, nameKeys, key);
75+
String value = XmlUtils.getNodeValue(element, nameKeys, valueKeys, key);
7476
return XmlUtils.toInt(value);
7577
}
7678

7779
@Override
7880
public long getLong(String key) {
79-
String value = XmlUtils.getNodeValue(element, nameKeys, key);
81+
String value = XmlUtils.getNodeValue(element, nameKeys, valueKeys, key);
8082
return XmlUtils.toLong(value);
8183
}
8284

8385
@Override
8486
public float getFloat(String key) {
85-
String value = XmlUtils.getNodeValue(element, nameKeys, key);
87+
String value = XmlUtils.getNodeValue(element, nameKeys, valueKeys, key);
8688
return XmlUtils.toFloat(value);
8789
}
8890

8991
@Override
9092
public double getDouble(String key) {
91-
String value = XmlUtils.getNodeValue(element, nameKeys, key);
93+
String value = XmlUtils.getNodeValue(element, nameKeys, valueKeys, key);
9294
return XmlUtils.toDouble(value);
9395
}
9496

9597
@Override
9698
public String getString(String key) {
97-
return XmlUtils.getNodeValue(element, nameKeys, key);
99+
return XmlUtils.getNodeValue(element, nameKeys, valueKeys, key);
98100
}
99101

100102
@Override
101103
public boolean has(String key) {
102-
return XmlUtils.getNodeValue(element, nameKeys, key) != null;
104+
return XmlUtils.getNodeValue(element, nameKeys, valueKeys, key) != null;
103105
}
104106

105107
@Override

okhttps-xml/src/main/java/com/ejlchina/okhttps/xml/XmlMsgConvertor.java

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
public class XmlMsgConvertor implements MsgConvertor, ConvertProvider {
2222

2323
private String[] nameKeys = {"name", "key"};
24+
private String[] valueKeys = {"value"};
2425

2526
private DocumentBuilderFactory dbFactory;
2627

@@ -57,13 +58,13 @@ private Element parseElement(InputStream in, Charset charset) {
5758
@Override
5859
public Mapper toMapper(InputStream in, Charset charset) {
5960
Element root = parseElement(in, charset);
60-
return new XmlMapper(nameKeys, root);
61+
return new XmlMapper(nameKeys, valueKeys, root);
6162
}
6263

6364
@Override
6465
public Array toArray(InputStream in, Charset charset) {
6566
Element root = parseElement(in, charset);
66-
return new XmlArray(nameKeys, root.getChildNodes());
67+
return new XmlArray(nameKeys, valueKeys, root.getChildNodes());
6768
}
6869

6970
@Override
@@ -94,6 +95,14 @@ public void setNameKeys(String[] nameKeys) {
9495
this.nameKeys = nameKeys;
9596
}
9697

98+
public String[] getValueKeys() {
99+
return valueKeys;
100+
}
101+
102+
public void setValueKeys(String[] valueKeys) {
103+
this.valueKeys = valueKeys;
104+
}
105+
97106
public DocumentBuilderFactory getDbFactory() {
98107
return dbFactory;
99108
}

okhttps-xml/src/main/java/com/ejlchina/okhttps/xml/XmlUtils.java

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -62,23 +62,28 @@ public static List<Element> findElements(NodeList nodes, String[] nameKeys, Stri
6262
return elements;
6363
}
6464

65-
public static String getNodeValue(Element element, String[] nameKeys, String key) {
65+
public static String getNodeValue(Element element, String[] nameKeys, String[] valueKeys, String key) {
6666
String value = element.getAttribute(key);
6767
if (!isBlank(value)) {
6868
return value;
6969
}
7070
NodeList children = element.getChildNodes();
7171
Element ele = findElement(children, nameKeys, key);
7272
if (ele != null) {
73-
return value(ele);
73+
return value(ele, valueKeys);
7474
}
7575
return null;
7676
}
7777

78-
private static String value(Node node) {
79-
String value = ((Element) node).getAttribute("value");
80-
if (!isBlank(value)) {
81-
return value;
78+
public static String value(Node node, String[] valueKeys) {
79+
if (node instanceof Element) {
80+
Element ele = (Element) node;
81+
for (String valueKey : valueKeys) {
82+
String value = ele.getAttribute(valueKey);
83+
if (!isBlank(value)) {
84+
return value;
85+
}
86+
}
8287
}
8388
return node.getTextContent();
8489
}

0 commit comments

Comments
 (0)