前端之家收集整理的这篇文章主要介绍了
XML工具类,
前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
对xml常用操作做了简单封装
public class XMLUtils {
public Document create_doc(){
Document doc = createDocument();
return doc;
}
public Document str_to_doc(String xmlStr) {
Document document = null;
try {
document = parseText(xmlStr);
} catch (DocumentException e) {
e.printStackTrace();
}
return 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;
}
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;
}
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;
}
public String xml_to_Str(Document doc) {
String documentStr = doc.asXML();
return documentStr.toString();
}
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;
}
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();
}
return list;
}
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;
}
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;
}
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获取指定节点下的内容。