对xml常用操作做了简单封装
public class XMLUtils {
//-----------------------------------------------------------------document start
/*创建document*/
public Document create_doc(){
Document doc = createDocument();
return doc;
}
/*从str转换到document*/
public Document str_to_doc(String xmlStr) {
Document document = null;
try {
document = parseText(xmlStr);
} catch (DocumentException e) {
e.printStackTrace();
}
return document;
}
/*从inputstream转换到document*/
public Document is_to_doc(InputStream is){
Document document = null;
SAXReader saxReader = new SAXReader();
try {
document = saxReader.read(is);
} catch (DocumentException e) {
e.printStackTrace();
}
return document;
}
/*从XML文件转换到document*/
public Document file_to_doc(String fileName) {
SAXReader reader = new SAXReader();
Document document = null;
try {
document = reader.read(new File(fileName));
} catch (DocumentException e) {
e.printStackTrace();
}
return document;
}
/*从URL流构建dom4j document对象*/
public Document url_to_doc(URL url){
Document document = null;
try{
SAXReader sr = new SAXReader();
document = sr.read(url);
} catch (DocumentException e) {
e.printStackTrace();
}
return document;
}
/*将document转换为String*/
public String xml_to_Str(Document doc) {
String documentStr = doc.asXML();
return documentStr.toString();
}
/*将Document写入到file*/
public boolean write_to_file(Document doc, File file) throws IOException {
boolean flag = true;
OutputFormat outFmt = new OutputFormat();
outFmt.setEncoding("UTF-8");
XMLWriter writer = new XMLWriter(new FileOutputStream(file), outFmt);
writer.write(doc);
writer.close();
return flag;
}
//------------------------------------------------------------------document end
/*获得root元素,默认编码为utf-8*/
public Element Ele_root(Document document) {
return document.getRootElement();
}
/*根据节点名获得第一个子节点*/
public List Ele_first(Element element){
List list = new ArrayList<>();
Iterator it = element.elementIterator();
while(it.hasNext()){
Element ele = (Element)it.next();
list.add(ele.getName());
}
return list;
}
/*获取指定节点所有属性值*/
public Map<String, String> Ele_attr(Element root, String xpath){
HashMap map = new LinkedHashMap();
List list = root.selectNodes("//" + xpath);
Iterator it = list.iterator();
while (it.hasNext()) {
Element ele = (Element)it.next();
for (Iterator i = ele.attributeIterator(); i.hasNext(); ) {
Attribute attribute = (Attribute)i.next();
map.put(attribute.getValue(), ((Attribute) i.next()).getValue());
}
}
return map;
}
/*获取指定节点列表*/
public List Elelist(Element root, String xpath){
List list = new ArrayList<Attribute>();
List element = root.selectNodes("//" + xpath);
Iterator it = element.iterator();
while (it.hasNext()) {
Element ele = (Element)it.next();
//do something
//attrs = getAttr(ele);
}
return list;
}
/*获取指定名称的属性---(Condition部分) ("//@op")*/
public List getAttr(Element element){
List list = new ArrayList<>();
List arry = element.selectNodes("//@name | //@value");
Iterator iter = arry.iterator();
while(iter.hasNext()){
Attribute attribute=(Attribute)iter.next();
list.add(attribute.getData());
}
return list;
}
/*获取指定节点下所有子节点的信息*/
public List Ele_list(Element element){
List list = new ArrayList();
Iterator it = element.elementIterator();
while(it.hasNext()){
Element ele = (Element)it.next();
if(ele.elements().size() > 0)
list.add(Ele_list(ele));
else
list.add(ele.getText());
}
return list;
}
/*获取指定节点下所有子节点的信息---返回list*/
public List<Map> Ele_map(Element element){
List list = new ArrayList<>();
for (Iterator it = element.elementIterator(); it.hasNext(); ){
Element ele = (Element)it.next();
Map map = new HashMap<>();
for (Iterator iterator = ele.elementIterator(); iterator.hasNext(); ){
Element itter = (Element)iterator.next();
map.put(itter.getName(), itter.getText());
}
list.add(map);
}
return list;
}
/*根据节点名称得到指定的子节点*/
public Map Ele_child(Element element, String childName){
Element ele = element.element(childName);
Map map = new HashMap<>();
for (Iterator iterator = ele.elementIterator(); iterator.hasNext(); ){
Element itter = (Element)iterator.next();
map.put(itter.getName(), itter.getText());
}
return map;
}
/*使用xpath获取指定节点*/
public Map Ele_xPath(Element root, String nodePath) {
Map map = new LinkedHashMap<>();
Element ele = (Element) root.selectSingleNode(nodePath);
List list = ele.elements();
for (Object obj : list) {
Element element = (Element) obj;
map.put(element.getName(), element.getTextTrim());
}
return map;
}
/*判断节点是否还有子节点*/
public boolean hasChildNode(Element element){
if (null == element)
return false;
return element.hasContent();
}
/*去掉声明头*/
public String cutDecHeader(String xmlStr){
return xmlStr.replaceFirst("\\s*<[^<>]+>\\s*", "");
}
}
xml解析后,将解析的节点放到Map或者Array中。用到xpath获取指定节点下的内容。