XML心得:
一般获取xml的document开始解析。每个属性都有一个Attribute类。每个Attribute类都可以获取其element类Element e=attribute.getParent();
遍历的时候有专门的elementIterator()方法.
package com.birtsample.util;
import java.io.File;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import org.dom4j.Attribute;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
public class XmlParseUtils {
public static HashMap getXmlStyle(String filePath,String styleName)
{
SAXReader saxReader = new SAXReader();
HashMap map=new HashMap();
try {
Document document = saxReader.read(new File(filePath));
//找到节点属性是"name"的路径
List list = document.selectNodes("/styles/style/@name" );
Iterator it = list.iterator();
List list2=null;
while(it.hasNext())
{
Attribute attribute = (Attribute)it.next();
//节点属性是"name"=特定值
if(attribute.getValue().equalsIgnoreCase(styleName))
{
//通过属性获取其element类
Element e=attribute.getParent();
Iterator it2=null;
it2=e.elementIterator();
while(it2.hasNext())
{
Element element=(Element)it2.next();
// System.out.println(element);
//通过element获取其属性或者他下节点
Attribute attr=element.attribute("name");
map.put(getMethodName(attr.getValue()), element.getText());
}
}
}
return map;
} catch (DocumentException e) {
// TODO 自动生成 catch 块
e.printStackTrace();
return null;
}
}
public static String getMethodName(String field)
{
String methodName=field.substring(1);
String upperCase=field.substring(0,1).toUpperCase();
methodName="set"+upperCase+methodName;
return methodName;
}
public static void main(String arg[])
{
getMethodName("fontFamily");
}
}
.import java.util.*;
import java.io.File;
import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.DocumentException;
import org.dom4j.io.SAXReader;
import org.dom4j.Node;
import org.dom4j.DocumentHelper;
import org.dom4j.Attribute;
public class Dom4jParseXml {
//通过xml文件名得到DOM
public Document getDocument(String xmlFileName) throws DocumentException {
SAXReader reader = new SAXReader();
Document d = reader.read(new File(xmlFileName));
return d;
}
//重载,通过xml文件内容得到DOM
public Document getDocument(String xmlContent, boolean b) throws
DocumentException {
Document d = DocumentHelper.parseText(xmlContent);
return d;
}
//输出字符串
public String transformDOM(Document d) {
String xmlContent = "";
xmlContent = d.asXML();
return xmlContent;
}
//得到节点
public Element getNode(Document d, String elePath, String eleValue) {
Element ele = null;
List l = d.selectNodes(elePath);
Iterator iter = l.iterator();
while (iter.hasNext()) {
Element tmp = (Element) iter.next();
if (tmp.getText().equals(eleValue)) {
ele = tmp;
}
}
return ele;
}
//重载,得到节点
public Element getNode(Document d, String eleName) {
Element ele = (Element) d.selectSingleNode(eleName);
return ele;
}
//增加节点
public void addNode(Element parentEle, String eleName, String eleValue) {
Element newEle = parentEle.addElement(eleName);
newEle.setText(eleValue);
}
//增加属性节点
public void addAttribute(Element ele, String attributeName,
String attributeValue) {
ele.addAttribute(attributeName, attributeValue);
}
//删除节点
public void removeNode(Element parentEle, String eleName, String eleValue) {
Iterator iter = parentEle.elementIterator();
Element delEle = null;
while (iter.hasNext()) {
Element tmp = (Element) iter.next();
if (tmp.getName().equals(eleName) && tmp.getText().equals(eleValue)) {
delEle = tmp;
}
}
if (delEle != null) {
parentEle.remove(delEle);
}
}
//删除属性
public void removeAttr(Element ele, String attributeName) {
Attribute att = ele.attribute(attributeName);
ele.remove(att);
}
//修改节点值
public void setNodeText(Element ele, String newValue) {
ele.setText(newValue);
}
//修改属性值
public void setAttribute(Element ele, String attributeName,
String attributeValue) {
Attribute att = ele.attribute(attributeName);
att.setText(attributeValue);
}
}
posted on 2007-04-19 14:39
Dragonofson 阅读(1251)
评论(0) 编辑 收藏