Last active
December 11, 2015 15:09
-
-
Save meigesir/4619266 to your computer and use it in GitHub Desktop.
解析XML时,处理相关XML节点的帮助类,记录下来以提升效率..(需借助dom4j.jar)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package thp.transition.utils; | |
| import java.io.BufferedWriter; | |
| import java.io.File; | |
| import java.io.FileOutputStream; | |
| import java.io.IOException; | |
| import java.io.OutputStreamWriter; | |
| import org.dom4j.DocumentFactory; | |
| import org.dom4j.Element; | |
| public class DocumentUtils { | |
| /** | |
| * 根据元素名称来获取该元素的值:如果没有此元素,那么返回为null | |
| * @param supE 父元素 | |
| * @param nodename | |
| * @return | |
| */ | |
| public static Object findValueByNodename(Element supE,String nodename){ | |
| Object obj = null; | |
| Element e = (Element) supE.element(nodename); | |
| if(e != null){ | |
| obj = e.getText(); | |
| } | |
| return obj; | |
| } | |
| /** | |
| * 创建有下面子元素的节点并返回 | |
| * @param factory | |
| * @param nodeName | |
| * @param nodeArray 存放子节点名称,创建自定义子节点 | |
| * @param args 存放到相应子节点中的值 | |
| * @return | |
| */ | |
| public static Element createNodeByNodename(DocumentFactory factory,String nodeName, | |
| Object[] nodeArray,Object... args) { | |
| if(args != null){ | |
| String[] strUtil = (String[])nodeArray; | |
| Element e = factory.createElement(nodeName); | |
| /*循环创建元素并赋值*/ | |
| for(int i=0;i < args.length;i++){ | |
| Element subE = createNodeAndSetValue(strUtil[i], args[i], factory); | |
| addSubElement(e, subE); | |
| } | |
| if(e.elements() == null || e.elements().size() == 0){ | |
| return null; | |
| } | |
| return e; | |
| } | |
| return null; | |
| } | |
| /** | |
| * 利用节点名称和节点值来创建并返回节点,如果节点值为null,那么返回值为null | |
| * @param nodeName 节点名称 | |
| * @param nodeValue 节点值 | |
| * @param factory 用户创建节点 | |
| * @return Element | |
| */ | |
| public static Element createNodeAndSetValue(String nodeName,Object nodeValue, | |
| DocumentFactory factory){ | |
| if(nodeValue != null){ | |
| Element e = factory.createElement(nodeName); | |
| e.setText(String.valueOf(nodeValue)); | |
| return e; | |
| } | |
| return null; | |
| } | |
| /** | |
| * 利用节点名称和属性名称和值来创建并返回节点,如果属性值为null,那么返回值为null | |
| * @param nodeName 节点名称 | |
| * @param nodeAttr 属性名称 | |
| * @param nodeValue 属性值 | |
| * @param factory 用户创建节点 | |
| * @return Element | |
| */ | |
| public static Element createNodeAndSetAttr(String nodeName,Object nodeAttr,Object nodeValue, | |
| DocumentFactory factory){ | |
| if(nodeValue != null){ | |
| Element e = factory.createElement(nodeName); | |
| e.addAttribute(String.valueOf(nodeAttr), String.valueOf(nodeValue)); | |
| return e; | |
| } | |
| return null; | |
| } | |
| /** | |
| * 向指定节点添加子节点,如果子节点为null,则不添加 | |
| * @param element 指定节点 | |
| * @param sub_E 子节点 | |
| */ | |
| public static void addSubElement(Element element, Element subE) { | |
| if(subE != null){ | |
| element.add(subE); | |
| } | |
| } | |
| /** | |
| * 将字符串类型的xml读进文件中 | |
| * @param path 要读进的文件的路径 | |
| * @param str 要读进的字符串 | |
| */ | |
| public static void readXmlToFile(String path,String str){ | |
| File file = new File(path); | |
| try { | |
| FileOutputStream fos = new FileOutputStream(file); | |
| OutputStreamWriter osw = new OutputStreamWriter(fos,"UTF-8"); | |
| BufferedWriter bw = new BufferedWriter(osw); | |
| try { | |
| bw.write(str); | |
| bw.flush(); | |
| bw.close(); | |
| } catch (IOException e) { | |
| e.printStackTrace(); | |
| } | |
| } catch (Exception e) { | |
| e.printStackTrace(); | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment