cxml
漆雕唯
2023-12-01
现在开发Java程序常常得写xml文档,包括许多配置文件,还有R/O映射,部署描述符……有时候真是觉得写的xml比Java程序都多,感觉很累。这有两个原因:首先,现在xml用得很多;第二,他不是按照编程语言来设计的,所以写起来很累。xml不是用来写代码的编程语言,当然不会像Java那样,写起来那么方便。最近,我有了一个简化xml语言想法,还很不成熟。简单的说,就是把xml语言按照Java这些编程语言的方式代码话,从而达到易写、易读、易于修改的目的。
到底怎么改呢?基于下面的几个原则:一、取消元素名称在首尾的重复输入;二、使用编程中重用的“(){};”等字符作为分割;三、用语句块代替元素层次。下面举例来说:
<element/>改为element;
<element p1=123 p2="abc"/>改为element(p1=123 p2=abc);
<parent><child/></parent>改为parent child;
<parent><child1/><child2/></parent>改为parent{child1;child2}
这种表示方法,我打算称它为cxml。意为code xml。现在,它还有很多内容不支持,有许多地方需要改进,可以说是极端简陋。我编写了xml和cxml之间的简易转换程序,希望不要笑话:
//将DOM转换为cxml
import org.w3c.dom.*;
import java.io.*;
import javax.xml.parsers.*;
public class DOM_CXML {
public static void outputElement(Element el,PrintStream out,StringBuffer head){
out.print(head.toString());
out.print(el.getNodeName());
if(el.hasAttributes()){
out.print('(');
out.print(' ');
NamedNodeMap am = el.getAttributes();
for(int i=0;i<am.getLength();i++){
Node n = am.item(i);
out.print(n.getNodeName());
out.print('=');
out.print(n.getNodeValue());
out.print(' ');
}
out.print(')');
}
if(el.hasChildNodes()){
out.println('{');
NodeList cl = el.getChildNodes();
for(int j=0;j<cl.getLength();j++){
if(cl.item(j).getNodeType()==Element.ELEMENT_NODE)
outputElement((Element)(cl.item(j)),out,head.append(' '));
}
out.print(head.toString());
out.println('}');
}else out.println(';');
if(head.length()>0) head.deleteCharAt(head.length()-1);
}
}
//将cxml转换为DOM
import org.w3c.dom.*;
import java.io.*;
import java.util.*;
import javax.xml.parsers.*;
public class CXML_DOM {
public static List buildElements(List words,Document doc,int start,int end){
List result = new LinkedList();
while(start<end){
Element buffer = doc.createElement((String)words.get(start));
result.add(buffer);
String ft = (String)words.get(start+1);
if(ft.equals(";")) {
start+=2;
continue;
}
else if(ft.equals("(")){
int e = search(words,start+1,")","(");
if(e<0) return null;
buildProperty(words,buffer,start+2,e);
String st = (String)words.get(e+1);
if(st.equals(";")){
start=e+2;
continue;
}
else if(st.equals("{")){
int ge = search(words,e+2,"}","{");
if(ge<0) return null;
List children = buildElements(words,doc,e+2,ge-1);
for(int j=0;j<children.size();j++)
buffer.appendChild((Node)children.get(j));
start = ge+1;
continue;
}
else return null;
}
else return null;
}
return result;
}
private static int search(List words,int start,String it,String unit){
int now = start+1;
int count = 0;
while(now<words.size()){
if(words.get(now).equals(it)){
count--;
if(count<0) return now;
}
else if(words.get(now).equals(unit)) count++;
now++;
}
return -1;
}
private static void buildProperty(List words,Element it,int start,int end){
while(start<end){
it.setAttribute((String)words.get(start)
,(String)words.get(start+2));
start+=3;
}
}
private static List caifen(StringReader reader) throws IOException{
List result = new LinkedList();
StringBuffer buffer = new StringBuffer();
while(true){
int ri = reader.read();
if(ri<0){
if(buffer.length()>0) result.add(buffer.toString());
return result;
}
char now = (char)ri;
switch(now){
case ' ': case '/n': case '/t':
if(buffer.length()>0){
result.add(buffer.toString());
buffer=new StringBuffer();
}
break;
case '(': case ')': case '{': case '}': case '=': case ';':
if(buffer.length()>0){
result.add(buffer.toString());
buffer=new StringBuffer();
}
result.add(String.valueOf(now));
break;
default:
buffer.append(now);
}
}
}
}
有的人说了,你这么懒,干脆别干了!我真心希望这样想的人,千万不要回到汇编的时代。