xml 2

来源:互联网 发布:淘宝化妆品有假货吗 编辑:程序博客网 时间:2024/05/21 09:43

// 查找menuroot,整个xml文档的菜单跟节点。
Node menuroot = doc.getDocumentElement();


//获得跟的有所节点
NodeList menuNodes = menuroot.getChildNodes();


//获得某个节点
Node node=menuNodes.item(i);

//获得某个节点的所有属性的map
NamedNodeMap att = node.getAttributes();


//从节点属性map中读出指定的属性的值
String value=att.getNamedItem("propertyName").getNodeValue();

//删除一个节点
Node n;
n.getParentNode().removeChild(n);

//根据文件名字,xml文件的读取
Document doc;
InputStream is = null;
is = new FileInputStream(getFile(url));
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
doc = db.parse(is);

//获得某个路径下的文件
public static File getFile(String name) {
  if (file != null) {
   return file;
  } else {
   String path = MenuUtil.class.getResource("/").getPath();
   path = path.substring(0, path.indexOf("WEB-INF/"));
   String filename = path + "conf/" + name;
   return new File(filename);
  }
 } 

原创粉丝点击