XMLObject 简易xml格式接口数据生成和解析工具

时间:2023-03-08 15:19:14
XMLObject 简易xml格式接口数据生成和解析工具

简单的请求封装,相当于封装了Book对象,但比Book对象参数更灵活

XMLObject xml = new XMLObject("book");
xml.add("author", "Jack");
xml.add("name", "JavaScript");
XMLObject store = new XMLObject("store");
store.add("city", "NewYourk");
xml.add(store);
System.out.println(xml.toString());
//<book><author>Jack</author><name>JavaScript</name><store><city>NewYourk</city></store></book>

包含多个store元素的封装,

store = store.clone(); //复制store,否则之前的会被覆盖
store.update("city", "Washington"); //更新已有元素city的值,NewYourk => Washington
store.add("country", "America"); //第二个store可以灵活地添加元素
xml.add(store);
System.out.println(xml.toString());
//<book><author>Jack</author><name>JavaScript</name><store><city>NewYourk</city></store><store><city>Washington</city><country>America</country></store></book>

解析xml,获取值

xml = XMLObject.fromString(xml.toString());
System.out.println(xml.get("name")); //JavaScript
List<XMLObject> nodes = xml.getNodes("store");
for(XMLObject node : nodes) {
System.out.println(node.toString());
}
//<store><city>NewYourk</city></store>
//<store><city>Washington</city><country>America</country></store>
public class XMLObject implements Cloneable {
private String name;
private List<Object> nodes = new LinkedList<>(); /**
* @param name 不能包含特殊字符</>
*/
public XMLObject(String name) {
if(name!=null && name.matches("[^</>]+")) {
this.name = name;
}else {
throw new IllegalArgumentException("bad name: "+name);
}
}
public String getName() {
return name;
}
public void add(String field, String value) {
nodes.add(new FieldValue(field, value));
}
public void add(XMLObject node) {
nodes.add(node);
}
public boolean update(String field, String value) {
for(Object node : nodes) {
if(node instanceof FieldValue) {
FieldValue fv = (FieldValue)node;
if(fv.getField().equals(field)) {
fv.value = value;
return true;
}
}
}
return false;
}
public String get(String field) {
for(Object node : nodes) {
if(node instanceof FieldValue) {
FieldValue fv = (FieldValue)node;
if(fv.getField().equals(field))
return fv.getValue();
}
}
return null;
}
public XMLObject getNode(String field) {
for(Object node : nodes) {
if(node instanceof XMLObject) {
XMLObject obj = (XMLObject)node;
if(obj.name.equals(field))
return obj;
}
}
return null;
}
public List<XMLObject> getNodes(String field) {
List<XMLObject> objs = new LinkedList<>();
for(Object node : nodes) {
if(node instanceof XMLObject) {
XMLObject obj = (XMLObject)node;
if(obj.name.equals(field))
objs.add(obj);
}
}
return objs;
} public String toString() {
StringBuilder xml = new StringBuilder("<"+name+">");
for(Object node : nodes) {
if(node instanceof FieldValue) {
FieldValue fv = (FieldValue)node;
xml.append("<"+fv.field+">");
xml.append(fv.value);
xml.append("</"+fv.field+">");
}else if(node instanceof XMLObject) {
XMLObject obj = (XMLObject)node;
xml.append(obj.toString());
}
}
xml.append("</"+name+">");
return xml.toString();
} protected XMLObject clone() {
XMLObject xml = new XMLObject(name);
xml.nodes = new LinkedList<>();
for(Object node : this.nodes) {
if(node instanceof FieldValue) {
FieldValue fv = (FieldValue)node;
xml.add(fv.field, fv.value);
}else if(node instanceof XMLObject) {
XMLObject obj = (XMLObject)node;
obj = obj.clone();
xml.add(obj);
}
}
return xml;
}
public static String header() {
return "<?xml version=\"1.0\" encoding=\"UTF-8\"?>";
}
public static XMLObject fromString(String xml) {
XMLObject obj = null;
StringBuilder field = new StringBuilder(), value = new StringBuilder();
if(xml.startsWith(header())) xml = xml.substring(header().length());
boolean tag = false, start = false;
char[] cs = xml.toCharArray();
for(int i = 0; i<cs.length; i++) {
char c = cs[i];
switch(c) {
case '<':
tag = true;
start = true;
if(field.length()>0 && value.length()==0) {
int end = xml.indexOf("</"+field+">", i);
String subXml = xml.substring(i-field.length()-2, end+field.length()+3);
XMLObject sub = fromString(subXml);
obj.add(sub);
i = end+field.length()+3;
field.delete(0, field.length());
}
break;
case '>':
tag = false;
if(start) {
if(obj==null) {
obj = new XMLObject(field.toString());
field.delete(0, field.length());
}
}else {
if(field.length()>0 && field.length()>0) {
obj.add(field.toString(), value.toString());
field.delete(0, field.length());
value.delete(0, value.length());
}
}
break;
case '/':
start = false;
break;
default:
if(tag && start) field.append(c) ;
if(!tag) value.append(c);
break;
}
}
return obj;
} public static class FieldValue {
private String field;
private String value;
/**
* @param field 不能包含特殊字符</>
* @param value 不能包含特殊字符</>
*/
public FieldValue(String field, String value) {
if(field!=null && value!=null) {
boolean b = field.matches("[^</>]+");
if(b) b = value.matches("[^</>]+");
else throw new IllegalArgumentException("bad field: "+field); if(b) { this.field = field; this.value = value; }
else throw new IllegalArgumentException("bad value: "+value);
}else {
throw new IllegalArgumentException("bad field or value: "+field+","+value);
}
}
public String getField() {
return field;
}
public String getValue() {
return value;
}
}
}
XMLObject 简易xml格式接口数据生成和解析工具 原文地址:http://www.xlongwei.com/detail/15032713