XMLRead

------ procedure.xml------

 

<? xml version="1.0" encoding="UTF-8" ?>
- < root >
- < procedure id =" sp_reg " desc =" 注册功能 " >
  < name > CALL sp_reg(?,?) </ name >
  < param index =" 1 " type =" Integer " />
  < param index =" 2 " type =" String " />
  </ procedure >
- < procedure >
  < name > CALL sp_login(?,?,?) </ name >
  < param index =" 1 " type =" Integer " />
  < param index =" 2 " type =" Integer " />
  < param index =" 3 " type =" String " inOutType =" out " />
  </ procedure >
- < procedure >
  < name > CALL sp_update_sal(?,?,?) </ name >
  < param index =" 1 " type =" Integer " />
  < param index =" 2 " type =" String " />
  < param index =" 3 " type =" ResultSet " inOutType =" out " />
  </ procedure >
  </ root >
 
-----------------------

package com.lovo.test;

import java.util.List;

import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.Node;
import org.dom4j.io.SAXReader;

/**
 dom4j-1.6.1.jar
 jaxen-1.1-beta-6.jar
 */
public class ReadXml {
 
 public static void main(String[] args) throws Exception{
  //得到一个读取器(固定)
  SAXReader sax = new SAXReader();
  //根据类的路径 读取 文件
  Document doc =sax.read(ReadXml.class.getResourceAsStream("/procedure.xml"));
  //指定要查找的路径,根路径用两个/
  String xpath = "//root/procedure";
  //执行查询,返回多个节点放到集合中
  List<Element> list = doc.selectNodes(xpath);
  //集合循环
  for(Element e:list){
   //得到当前节点的(第1个)name子节点的文本
   String name = e.elementText("name");
   System.out.println(name);
   //得到当前节点所有叫 param 的子节点
   List<Element> listChild = e.elements("param");
   //循环
   for(Element e2:listChild){
    //节点的属性通常用 valueOf("@属性名")
    System.out.println(e2.valueOf("@index")+","+e2.valueOf("@type")+","+e2.valueOf("@inOutType"));
   }
  }
  //根据属性直接定位到某一个节点
  String xpath2 = "//root/procedure[@id='sp_reg']";
  //返回一个节点,Node和Element其实是相同的
  Node node = doc.selectSingleNode(xpath2);
//  Element ele = (Element)node;
  System.out.println(node.valueOf("@desc"));
 }

}

 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
解释分析细致讲解一下这段代码int ncds_file_editconfig_internal (struct ncds_ds *ds, NC_DATASTORE target, char config) { struct ncds_ds_file * file_ds = (struct ncds_ds_file )ds; xmlDocPtr config_doc, datastore_doc; xmlNodePtr target_ds, tmp_target_ds, aux_node, root; int retval = EXIT_SUCCESS, ret; char aux = NULL; const char configp; LOCK(file_ds,ret); if (ret) { return EXIT_FAILURE; } DBG("enter %s/%d\n", func,LINE); if(file_fill_dsnodes(file_ds)) { UNLOCK(file_ds); ERROR("%s: file_ds->running_all/startup_all/candidate_all is NULL\n", func); return EXIT_FAILURE; } DBG("%s step1\n", func); file_rollback_store(file_ds); switch(target) { case NC_DATASTORE_RUNNING: target_ds = file_ds->running; break; case NC_DATASTORE_STARTUP: target_ds = file_ds->startup; break; case NC_DATASTORE_CANDIDATE: target_ds = file_ds->candidate; break; default: UNLOCK(file_ds); ERROR("%s: invalid target.", func); return EXIT_FAILURE; break; } if (strncmp(config, "<?xml", 5) == 0) { if ((configp = strchr(config, '>')) == NULL) { UNLOCK(file_ds); ERROR("%s: invalid config.", func); return EXIT_FAILURE; } ++configp; while (*configp == ' ' || *configp == '\n' || configp == '\t') { ++configp; } } else { configp = config; } if (asprintf(&aux, "<config>%s</config>", configp) == -1) { UNLOCK(file_ds); ERROR("asprintf() failed (%s:%d).", FILE, LINE); return EXIT_FAILURE; } if ((config_doc = xmlReadMemory (aux, strlen(aux), NULL, NULL, NC_XMLREAD_OPTIONS)) == NULL) { UNLOCK(file_ds); free(aux); ERROR("%s: Reading xml data failed!", func); return EXIT_FAILURE; } free(aux); root = xmlDocGetRootElement(config_doc); for (aux_node = root->children; aux_node != NULL; aux_node = root->children) { xmlUnlinkNode(aux_node); xmlAddNextSibling(config_doc->last, aux_node); } aux_node = root->next; xmlUnlinkNode(root); xmlFreeNode(root); datastore_doc = xmlNewDoc (BAD_CAST "1.0"); xmlDocSetRootElement(datastore_doc, xmlCopyNode(target_ds->children, 1)); if (target_ds->children) { for (root = target_ds->children->next; root != NULL; root = aux_node) { aux_node = root->next; xmlAddNextSibling(datastore_doc->last, xmlCopyNode(root, 1)); } } retval = edit_config_internal(datastore_doc, config_doc, (struct ncds_ds)file_ds, NC_EDIT_DEFOP_NOTSET); if (EXIT_SUCCESS == retval) { #if 1 while ((aux_node = target_ds->children) != NULL) { xmlUnlinkNode(aux_node); xmlFreeNode(aux_node); } xmlAddChildList(target_ds, xmlCopyNodeList(datastore_doc->children)); if (file_sync(file_ds)) { retval = EXIT_FAILURE; } #endif } else { retval = EXIT_FAILURE; } UNLOCK(file_ds); xmlFreeDoc (datastore_doc); xmlFreeDoc (config_doc); return retval; }
最新发布
06-09
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值