[利用Dom4j解析]
<Root name="MC" displayName="MC Configuration">
<Moc Name="BRD" action="ADD,MOD,RMV,LST">
<Moc Name="SLOT" action="ADD,MOD,RMV,LST">
</Moc>
</Moc>
<Moc Name="AREA" action="ADD,MOD,RMV,LST">
</Moc>
</Root>
将上段解析成下段<Root name="MC" displayName="MC Configuration">
<Moc Name="BRD" displayName="Board Management">
<Command name="ADD BRD" displayName="Add Board" objName="BRD" cmdType="ADD"/>
<Command name="MOD BRD" displayName="Modify Board" objName="BRD" cmdType="MOD"/>
<Command name="RMV BRD" displayName="Remove Board" objName="BRD" cmdType="RMV"/>
<Command name="LST BRD" displayName="List Board" objName="BRD" cmdType="LST"/>
<Moc Name="SLOT" displayName="Slot Management">
<Command name="ADD SLOT" displayName="Add Slot" objName="SLOT" cmdType="ADD"/>
<Command name="MOD SLOT" displayName="Modify Slot" objName="SLOT" cmdType="MOD"/>
<Command name="RMV SLOT" displayName="Remove Slot" objName="SLOT" cmdType="RMV"/>
<Command name="LST SLOT" displayName="List Slot" objName="SLOT" cmdType="LST"/>
</Moc>
</Moc>
<Moc Name="AREA" displayName="Area Management">
<Command name="ADD AREA" displayName="Add Area" objName="AREA" cmdType="ADD"/>
<Command name="MOD AREA" displayName="Modify Area" objName="AREA" cmdType="MOD"/>
<Command name="RMV AREA" displayName="Remove Area" objName="AREA" cmdType="RMV"/>
<Command name="LST AREA" displayName="List Area" objName="AREA" cmdType="LST"/>
</Moc>
</Root>
<Root name="MC" displayName="MC Configuration">
<Moc Name="BRD" action="ADD,MOD,RMV,LST">
<Moc Name="SLOT" action="ADD,MOD,RMV,LST">
</Moc>
</Moc>
<Moc Name="AREA" action="ADD,MOD,RMV,LST">
</Moc>
</Root>
将上段解析成下段<Root name="MC" displayName="MC Configuration">
<Moc Name="BRD" displayName="Board Management">
<Command name="ADD BRD" displayName="Add Board" objName="BRD" cmdType="ADD"/>
<Command name="MOD BRD" displayName="Modify Board" objName="BRD" cmdType="MOD"/>
<Command name="RMV BRD" displayName="Remove Board" objName="BRD" cmdType="RMV"/>
<Command name="LST BRD" displayName="List Board" objName="BRD" cmdType="LST"/>
<Moc Name="SLOT" displayName="Slot Management">
<Command name="ADD SLOT" displayName="Add Slot" objName="SLOT" cmdType="ADD"/>
<Command name="MOD SLOT" displayName="Modify Slot" objName="SLOT" cmdType="MOD"/>
<Command name="RMV SLOT" displayName="Remove Slot" objName="SLOT" cmdType="RMV"/>
<Command name="LST SLOT" displayName="List Slot" objName="SLOT" cmdType="LST"/>
</Moc>
</Moc>
<Moc Name="AREA" displayName="Area Management">
<Command name="ADD AREA" displayName="Add Area" objName="AREA" cmdType="ADD"/>
<Command name="MOD AREA" displayName="Modify Area" objName="AREA" cmdType="MOD"/>
<Command name="RMV AREA" displayName="Remove Area" objName="AREA" cmdType="RMV"/>
<Command name="LST AREA" displayName="List Area" objName="AREA" cmdType="LST"/>
</Moc>
</Root>
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.MalformedURLException;
import java.util.Iterator;
import java.util.List;import org.dom4j.*;
import org.dom4j.io.*;public class MyDOM4JXMLReader {
public static void main(String[] args) throws DocumentException, IOException {
MyDOM4JXMLReader md = new MyDOM4JXMLReader();
String path = "";
if("".equals(path)){
path = "E:\\work\\simple.xml";
}
md.readAny(path); }
Document document = null;
public void test(List childNodes,Element root) throws IOException{
for(Object obj:childNodes) {//生成根节点
Node childNode = (Node)obj;
int j = childNode.selectNodes("*").size();
Element root4 = root.addElement("MOC");
String name9 = childNode.valueOf("@Name");
root4.addAttribute("name", name9);
root4.addAttribute("displayName", name9.substring(0,1)+name9.substring(1).toLowerCase()+" Management");
if(j>0){
//1个节点
List childNodes2 = childNode.selectNodes("*");
this.test(childNodes2,root4);
}
String name = childNode.valueOf("@Name");
String action = childNode.valueOf("@action");
root4.addAttribute("name", name.toUpperCase());
root4.addAttribute("displayName", name+" Management");
String[] actions = action.split(",");
for(int i=0;i<actions.length;i++){
Element root1 = root4.addElement("Command");
String actionN = actions[i];
String actionCmdType = actionN;
if("MOD".equals(actionN)){
actionN = "Modify";
}else if("RMV".equals(actionN)){
actionN = "Remove";
}else if("LST".equals(actionN)){
actionN = "List";
}
String name0 = name;
if("BRD".equals(name)){
name = "Board";
}
root1.addAttribute("name",actionN.toUpperCase()+" "+name0);
root1.addAttribute("displayName",actionN.substring(0,1)+actionN.substring(1).toLowerCase()+" "+name.substring(0,1)+name.substring(1).toLowerCase());
root1.addAttribute("objName",name);
root1.addAttribute("cmdType",actionCmdType);
}
}
XMLWriter writer = new XMLWriter(new java.io.OutputStreamWriter(new java.io.FileOutputStream("d://123.xml"),"UTF-8"));
writer.write(document);
writer.close();
}
public void readAny(String path) throws DocumentException, IOException{
//read
SAXReader reader = new SAXReader();
Document doc = reader.read(path);
document=DocumentHelper.createDocument();
Element root=document.addElement("Root");
String name = doc.getRootElement().valueOf("@name");
String displayName = doc.getRootElement().valueOf("@displayName");
root.addAttribute("name", name);
root.addAttribute("displayName", displayName);
List roots = doc.getRootElement().elements();
this.test(roots, root);
}
}
for(int i=0;i<actions.length;i++)
{
//生成newSimple.xml下MOC节点下的Command节点
Element rootCommand = rootMoc.addElement("Command");
//用变量actionN接收遍历出来的单个action元素
String actionN = actions[i];
//匹配类型修改值
String actionCmdType = actionN;
if("MODIFY".equals(actionN))
{
actionN = "MOD";
}
else if("REMOVE".equals(actionN))
{
actionN = "RMV";
}
else if("LIST".equals(actionN))
{
actionN = "LST";
}
//定义变量displayNameStart和displayNameEnd接收displayName的拼接字符串
String displayNameStart = actionN.substring(0,1)+actionN.substring(1).toLowerCase();
String displayNameEnd = name.substring(0,1)+name.substring(1).toLowerCase();
if("Mod".equals(displayNameStart))
{
displayNameStart = "Modify";
}
else if("Rmv".equals(displayNameStart))
{
displayNameStart = "Remove";
}
else if("Lst".equals(displayNameStart))
{
displayNameStart = "List";
}
if("Brd".equals(displayNameEnd))
{
displayNameEnd = "Board";
}
/**
* 为Command节点添加各属性
*/
rootCommand.addAttribute("name",actionN.toUpperCase()+" "+name);
rootCommand.addAttribute("displayName",displayNameStart+" "+displayNameEnd);
rootCommand.addAttribute("objName",name);
rootCommand.addAttribute("cmdType",actionCmdType);
}
}
/**
* 写出文件newSimple.xml
*/
XMLWriter writer = new XMLWriter(new java.io.OutputStreamWriter(new java.io.FileOutputStream("f://newSimple.xml"),"UTF-8"));
writer.write(document);
writer.close();
}
{
//read simple.xml
SAXReader reader = new SAXReader();
Document doc = reader.read(path);
document=DocumentHelper.createDocument();
Element root=document.addElement("Root");
String name = doc.getRootElement().valueOf("@name");
String displayName = doc.getRootElement().valueOf("@displayName");
root.addAttribute("name", name);
root.addAttribute("displayName", displayName);
List roots = doc.getRootElement().elements();
this.buildXML(roots, root);
}
}
http://www.blogjava.net/biiau/archive/2008/09/24/231005.html
回顾到2个月前,进项目组时,对所有的一切都很陌生,自己也不知道一些规矩。只好慢慢在其中摸索。PL给我定位是使用DS(桌面服务器)与前后台交互。前台:web(界面显示层)。在一个项目中web是客户体验的地方,每一个控件,每一种布局都是需要精确的。我还记得初步开发时,同事们都还在笑界面不美观等样式问题。确实对于web界面的开发,我们不能忽视,因为这正是那些用户所关注的要点之一。当然,对
目标的实现。
,开发重点,以及协调都无疑是一种非常实用的方式。我想这也是符合敏捷开发这一特点的。数据处理后,DS将新生成的XML格式与数据以报文形式响应给web层。web再将其数据截取显示,那么这就是整个调用流程。流程分得很细,每个环节都是输入输出。这样我们出了问题时能够很快的定位到,问题也能马上得到解决。开始上手DS时,不了解,也不知道其意义。在本项目中作为中间件也是控制层,开始我从文档里的一些例子中与同事的代码中依样画葫芦,从模仿中去学习。这是学习的技巧,也是前人总结的经验。到现在,对DS这块有了概念,也知道了其作用。回头看看2个月前真是大有所长。
{
if(a==b)return true;
if(a.length()!=b.length()) return false;
for(int i=0;i <a.length();i++)
{
if(!(a.get(i).equles(b.get(i)))retrun false;
}
}
for(int i = 0; i < a.size; i++) {
for(int j = 0; j < b.size; j++) {
if(a.get(i).equals(b.get(i))) {
break;
}
else {
c.add(b.get(i));
break;
}
}
}
ArrayList c 是存储在B里面与A不同的对象;看看我这么写有问题吗?
for(int j = 0; j < b.size(); j++) {
if(a.get(i).equals(b.get(j)))
{a.remove(j);break;}
c.add(b.get(i));
}
}