免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 706 | 回复: 0
打印 上一主题 下一主题

xml总结 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2009-09-16 15:04 |只看该作者 |倒序浏览
DOM4J:
==================================================================================
1、写XML
    public void save() throws Exception {
        OutputFormat format = OutputFormat.createPrettyPrint();
        format.setEncoding("utf-8");
        OutputStream fos = new FileOutputStream(xmlFileName);
        XMLWriter writer = new XMLWriter(fos, format);
        writer.write(xmlDoc);
        writer.close();
    }
2、读取XML
InputStream is = new FileInputStream(config);//config:xml文件所在的路径
   SAXReader sax=new SAXReader();
   Document doc=sax.read(is);  
   List sourceList = doc.selectNodes("/data-source/source");//xml二级元素
   Iterator iterator = sourceList.iterator();
   while(iterator.hasNext()){
    Element source = (Element)iterator.next();
    DataSourceBean dsBean = new DataSourceBean();
    dsBean.setName(source.elementText("name"));
//    System.out.println(source.elementText("name"));
    dsBean.setUrl(source.elementText("url"));
                         }
==================================================================================
JDOM:
==================================================================================
1、写XML:
Format format = Format.getCompactFormat();
        format.setEncoding("utf-8");           //设置xml文件的字符为utf-8
        format.setIndent("    ");    //设置xml文件的缩进为4个空格
        XMLOutputter XMLOut = new XMLOutputter(format);
        /*
         * 将doc1中的各个节点及值放入文件fileName2中。
         */
     XMLOut.output(doc1, new FileOutputStream(fileName2));
2、形成DOC:
Element root1=new Element("video");
Document doc1=new Document(root1);//将根元素植入文档doc中
root1.setAttribute("allnum",String.valueOf(i));
遍历一个集合创建N个record节点
Element record =new Element("record");
Element id=new Element("id");
id.setText(modifyString(m.getId()));
record.addContent(id);
。。。
Element  name=new Element("name");
name.setText(modifyString(m.getName()));
record.addContent(name);
root1.addContent(record);
3、读取XML:
SAXBuilder saxBuilder = new SAXBuilder();
org.jdom.Document doc = saxBuilder.build(url);
Element root  = doc.getRootElement();
List allChild = root.getChildren();
Element el = (Element)allChild.get(i);
Element el1 =(Element) el.getChild("keyword");
Element el2 =(Element) el.getChild("path");
String keyword = el1.getText();
String path = el2.getText();
==================================================================================
自制作:
==================================================================================
1、写XML:
public static void writeXml(ImageStore[] ir, int allnum, PrintWriter writer)throws IOException
{
     writer.write("\n");
     if (ir == null || ir.length == 0)
        {
writer.write("no result");
return;
        }
    writer.write("\n");
    for (int i = 0; i \n");
writer.write("");
writer.write(ir.getKeyword());
writer.write("\n");
writer.write("");
writer.write(ir.getCurrank().toString());
writer.write("\n");
        writer.write("\n");
        }
  writer.write("\n");
}
2、读取XML:
public void parse(String content) {
  int start, end;
  start = content.indexOf("allnum=");
  if (start == -1) {
   allnum = 0;
  }else {
   allnum = Integer.parseInt(content.substring(start + 8, content.indexOf("'", start + 8)));
  }
  //首先根据头文件确定循环次数
  String[] records = parseTag(content, _RECORD);
  resultList = new ArrayList();
  for (int i = 0; i  0)) {
    sgr.setTitle(items[0]);
   }
   items = parseTag(records, _URL);
   if ((items != null) && (items.length > 0)) {
    sgr.setUrl(items[0]);
   }
   items = parseTag(records, _FORMAT);
   if ((items != null) && (items.length > 0)) {
    sgr.setFormat(items[0]);
   }
   items = parseTag(records, _SIZE);
                  。。。。。
   
   resultList.add(sgr);
  }
}
--------------------------------------
public static String[] parseTag(String content, String tag)
{
  final String _TAG_START = "";
  final String _TAG_END = "";
  ArrayList results = new ArrayList();
  int start = content.indexOf(_TAG_START);
  int end = 0;
  while (start >= 0) {
   end = content.indexOf(_TAG_END, start);
   if (end
   String item = content.substring(start + _TAG_START.length(), end);
   start = content.indexOf(_TAG_START, end);
   results.add(item);
  }
  return (String[]) results.toArray(new String[0]);
}
==================================================================================

本文来自ChinaUnix博客,如果查看原文请点:http://blog.chinaunix.net/u3/104127/showart_2053761.html
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP