
com.github.becausetesting.xml.DOM4JUtils Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of commons Show documentation
Show all versions of commons Show documentation
A common libraries used for testing framework.
/**
* Project Name:commons
* File Name:DOM4JUtils.java
* Package Name:com.github.becausetesting.xml
* Date:2016年4月16日下午8:34:56
* Copyright (c) 2016, [email protected] All Rights Reserved.
*
*/
package com.github.becausetesting.xml;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URL;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.Namespace;
import org.dom4j.Node;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.SAXReader;
import org.dom4j.io.XMLWriter;
import org.dom4j.xpath.DefaultXPath;
/**
* ClassName: DOM4JUtils
* Function: TODO ADD FUNCTION.
* Reason: TODO ADD REASON
* date: Apr 16, 2016 8:46:32 PM
*
* @author Administrator
* @version 1.0.0
* @since JDK 1.8
* @see SAXReader
*
*/
public class DOM4JUtils {
private String xmlfile;
private Document document=null;
public DOM4JUtils(String xmlfile){
this.xmlfile=xmlfile;
try {
//document = reader.read(new ByteArrayInputStream(xml.getBytes("UTF-8")));
document=new SAXReader().read(new File(xmlfile));
} catch (DocumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public DOM4JUtils(URL url){
try {
document=new SAXReader().read(url);
} catch (DocumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public Node getNode(String xpath){
Node node=null;
Element rootElement = document.getRootElement();
String namespace = rootElement.getNamespaceURI();
if(namespace!=null){
DefaultXPath defaultXPath=new DefaultXPath(xpath);
Map namespaces = new TreeMap();
namespaces.put("ns", namespace);
defaultXPath.setNamespaceURIs(namespaces);
node=defaultXPath.selectSingleNode(document);
}else{
node=rootElement.selectSingleNode(xpath);
}
return node;
}
public List getNodes(String xpath){
//xpath like: //ns:AuthResponse
List node=null;
Element rootElement = document.getRootElement();
String namespace = rootElement.getNamespaceURI();
if(namespace!=null){
DefaultXPath defaultXPath=new DefaultXPath(xpath);
Map namespaces = new TreeMap();
namespaces.put("ns", namespace);
defaultXPath.setNamespaceURIs(namespaces);
node=defaultXPath.selectNodes(document);
}else{
node=rootElement.selectNodes(xpath);
}
return node;
}
public String getNodeAttributeValue(String xpath,String attribute){
// or use the xpath to get the attribute value://foo/bar/author/@name
String attributeValue=null;
Element rootElement = document.getRootElement();
String namespace = rootElement.getNamespaceURI();
if(namespace!=null){
DefaultXPath defaultXPath=new DefaultXPath(xpath);
Map namespaces = new TreeMap();
namespaces.put("ns", namespace);
defaultXPath.setNamespaceURIs(namespaces);
attributeValue=defaultXPath.selectSingleNode(document).valueOf("@"+attribute);
}else{
attributeValue=rootElement.selectSingleNode(xpath).valueOf("@"+attribute);
}
return attributeValue;
}
public void setNodeValue(String xpath,String value){
Element rootElement = document.getRootElement();
String namespace = rootElement.getNamespaceURI();
if(namespace!=null){
DefaultXPath defaultXPath=new DefaultXPath(xpath);
Map namespaces = new TreeMap();
namespaces.put("ns", namespace);
defaultXPath.setNamespaceURIs(namespaces);
defaultXPath.selectSingleNode(document).setText(value);
}else{
rootElement.selectSingleNode(xpath).setText(value);
}
}
protected String getNode(String file,String nodename){
Document document=null;
String nodevalue=null;
try {
document=new SAXReader().read(new File(file));
Element rootElement = document.getRootElement();
System.out.println("root element: "+rootElement.attributeValue("name"));
List elements = rootElement.elements();
for (Iterator iter = rootElement.elementIterator(); iter.hasNext();)
{
Element e = (Element) iter.next();
System.out.println(e.attributeValue("name"));
}
return rootElement.elementText(nodename).trim();
} catch (DocumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return nodevalue;
}
public void writeSampleXml(){
//create the document
Document document = DocumentHelper.createDocument();
Element root = DocumentHelper.createElement("sample");
document.setRootElement(root);
Element firstelement = root.addElement("hello");
firstelement.setText("testhello");
firstelement.addAttribute("name", "tester");
Element secondelement = root.addElement("hello2");
secondelement.setText("testhello2: http://www.cnblogs.com/mengdd/archive/2013/06/05/3119927.html");
OutputFormat format=new OutputFormat(" ", true);
format.setEncoding("UTF-8");
try {
XMLWriter console=new XMLWriter(System.out,format);
console.write(document);
XMLWriter filewriter = new XMLWriter(new FileOutputStream(new File("sample.xml")), format);
filewriter.write(document);
filewriter.flush(); //must have this line
} catch (UnsupportedEncodingException | FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy