本文整理汇总了Java中org.dom4j.Document类的典型用法代码示例。如果您正苦于以下问题:Java Document类的具体用法?Java Document怎么用?Java Document使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
Document类属于org.dom4j包,在下文中一共展示了Document类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: parseXml
import org.dom4j.Document; //导入依赖的package包/类
/**
* @param @param request
* @param @throws Exception
* @Description: 解析微信发来的请求(XML)
*/
@SuppressWarnings("unchecked")
public static Map<String, String> parseXml(HttpServletRequest request) throws Exception {
// 将解析结果存储在 HashMap 中
Map<String, String> map = new HashMap<String, String>();
// 从 request 中取得输入流
InputStream inputStream = request.getInputStream();
// 读取输入流
SAXReader reader = new SAXReader();
Document document = reader.read(inputStream);
// 得到 xml 根元素
Element root = document.getRootElement();
// 得到根元素的所有子节点
List<Element> elementList = root.elements();
// 遍历所有子节点
for (Element e : elementList) {
map.put(e.getName(), e.getText());
}
// 释放资源
inputStream.close();
inputStream = null;
return map;
}
示例2: hadSubItemRight
import org.dom4j.Document; //导入依赖的package包/类
/**
* 验证是否具有子功能权限
*
* @param ds
* @param parentId
* @return
*/
private boolean hadSubItemRight(Document ds, int parentId)
{
List<Element> elments = ds.getRootElement().selectNodes("function[id!=0 and parentId=" + parentId + "]");
String strId = null;
for (Element item : elments)
{
strId = item.selectSingleNode("id").getText();
if (item.selectSingleNode("flag").getText().equals("1") || hadSubItemRight(ds, Integer.parseInt(strId)))
{
return true;
}
}
return false;
}
示例3: query_timestamp
import org.dom4j.Document; //导入依赖的package包/类
/**
* 用于防钓鱼,调用接口query_timestamp来获取时间戳的处理函数
* 注意:远程解析XML出错,与服务器是否支持SSL等配置有关
* @return 时间戳字符串
* @throws IOException
* @throws DocumentException
* @throws MalformedURLException
*/
public static String query_timestamp(AlipayConfig config) throws MalformedURLException,
DocumentException, IOException {
//构造访问query_timestamp接口的URL串
String strUrl = ALIPAY_GATEWAY_NEW + "service=query_timestamp&partner=" + config.getPartnerId() + "&_input_charset" +AlipayConfig.inputCharset;
StringBuffer result = new StringBuffer();
SAXReader reader = new SAXReader();
Document doc = reader.read(new URL(strUrl).openStream());
List<Node> nodeList = doc.selectNodes("//alipay/*");
for (Node node : nodeList) {
// 截取部分不需要解析的信息
if (node.getName().equals("is_success") && node.getText().equals("T")) {
// 判断是否有成功标示
List<Node> nodeList1 = doc.selectNodes("//response/timestamp/*");
for (Node node1 : nodeList1) {
result.append(node1.getText());
}
}
}
return result.toString();
}
示例4: Dom2Map
import org.dom4j.Document; //导入依赖的package包/类
@SuppressWarnings("unchecked")
public static Map<String, Object> Dom2Map(Document doc) {
Map<String, Object> map = new HashMap<String, Object>();
if (doc == null)
return map;
Element root = doc.getRootElement();
for (Iterator iterator = root.elementIterator(); iterator.hasNext();) {
Element e = (Element) iterator.next();
List list = e.elements();
if (list.size() > 0) {
map.put(e.getName(), Dom2Map(e));
} else
map.put(e.getName(), e.getText());
}
return map;
}
示例5: getVersionCode
import org.dom4j.Document; //导入依赖的package包/类
public static String getVersionCode(File androidManifestFile) throws IOException, DocumentException {
SAXReader reader = new SAXReader();
String versionCode = "";
if (androidManifestFile.exists()) {
Document document = reader.read(androidManifestFile);// Read the XML file
Element root = document.getRootElement();// Get the root node
if ("manifest".equalsIgnoreCase(root.getName())) {
List<Attribute> attributes = root.attributes();
for (Attribute attr : attributes) {
if (StringUtils.equalsIgnoreCase(attr.getName(), "versionCode")) {
versionCode = attr.getValue();
}
}
}
}
return versionCode;
}
示例6: parseAddonInfoLastSnapshotVersionMetaData
import org.dom4j.Document; //导入依赖的package包/类
@SuppressWarnings("rawtypes")
public Map<String, Object> parseAddonInfoLastSnapshotVersionMetaData(String xml) throws Exception {
InputStream inputStream = toInputStream(xml);
try {
Document document = saxReader.read(inputStream);
List list = document.selectNodes("//metadata/versioning/snapshot");
Iterator iter = list.iterator();
while (iter.hasNext()) {
Element element = (Element) iter.next();
Map<String, Object> map = new HashMap<String, Object>();
for (Iterator iterInner = element.elementIterator(); iterInner.hasNext();) {
Element elementInner = (Element) iterInner.next();
map.put(elementInner.getName(), elementInner.getText());
}
return map;
}
} finally {
inputStream.close();
}
return null;
}
示例7: parseXml2Map
import org.dom4j.Document; //导入依赖的package包/类
/**
* 解析XML并将其节点元素压入Dto返回(基于节点值形式的XML格式)
*
* @param pStrXml 待解析的XML字符串
* @param pXPath 节点路径(例如:"//paralist/row" 则表示根节点paralist下的row节点的xPath路径)
* @return outDto 返回Dto
*/
public static final Map parseXml2Map(String pStrXml, String pXPath) {
Map map = new HashMap();
String strTitle = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>";
Document document = null;
try {
if (pStrXml.indexOf("<?xml") < 0) pStrXml = strTitle + pStrXml;
document = DocumentHelper.parseText(pStrXml);
} catch (DocumentException e) {
logger.error("==开发人员请注意:==\n将XML格式的字符串转换为XML DOM对象时发生错误啦!" + "\n详细错误信息如下:", e);
}
// 获取根节点
Element elNode = document.getRootElement();
// 遍历节点属性值将其压入Dto
for (Iterator it = elNode.elementIterator(); it.hasNext();) {
Element leaf = (Element)it.next();
map.put(leaf.getName().toLowerCase(), leaf.getData());
}
return map;
}
示例8: parseMap2Xml
import org.dom4j.Document; //导入依赖的package包/类
/**
* 将Dto转换为符合XML标准规范格式的字符串(基于属性值形式)
*
* @param map 传入的Dto对象
* @param pRootNodeName 根节点名
* @param pFirstNodeName 一级节点名
* @return string 返回XML格式字符串
*/
public static final String parseMap2Xml(Map map, String pRootNodeName, String pFirstNodeName) {
Document document = DocumentHelper.createDocument();
// 增加一个根元素节点
document.addElement(pRootNodeName);
Element root = document.getRootElement();
root.addElement(pFirstNodeName);
Element firstEl = (Element)document.selectSingleNode("/" + pRootNodeName + "/" + pFirstNodeName);
Iterator keyIterator = map.keySet().iterator();
while (keyIterator.hasNext()) {
String key = (String)keyIterator.next();
String value = (String)map.get(key);
firstEl.addAttribute(key, value);
}
// 将XML的头声明信息丢去
String outXml = document.asXML().substring(39);
return outXml;
}
示例9: parseList2Xml
import org.dom4j.Document; //导入依赖的package包/类
/**
* 将List数据类型转换为符合XML格式规范的字符串(基于节点属性值的方式)
*
* @param pList 传入的List数据(List对象可以是Dto、VO、Domain的属性集)
* @param pRootNodeName 根节点名称
* @param pFirstNodeName 行节点名称
* @return string 返回XML格式字符串
*/
public static final String parseList2Xml(List pList, String pRootNodeName, String pFirstNodeName) {
Document document = DocumentHelper.createDocument();
Element elRoot = document.addElement(pRootNodeName);
for (int i = 0; i < pList.size(); i++) {
Map map = (Map)pList.get(i);
Element elRow = elRoot.addElement(pFirstNodeName);
Iterator it = map.entrySet().iterator();
while (it.hasNext()) {
Map.Entry entry = (Map.Entry)it.next();
elRow.addAttribute((String)entry.getKey(), String.valueOf(entry.getValue()));
}
}
String outXml = document.asXML().substring(39);
return outXml;
}
示例10: sendRequest
import org.dom4j.Document; //导入依赖的package包/类
protected void sendRequest(Document request) throws IOException {
try {
StringWriter writer = new StringWriter();
(new XMLWriter(writer,OutputFormat.createPrettyPrint())).write(request);
writer.flush(); writer.close();
SessionImplementor session = (SessionImplementor)new _RootDAO().getSession();
Connection connection = session.getJdbcConnectionAccess().obtainConnection();
try {
CallableStatement call = connection.prepareCall(iRequestSql);
call.setString(1, writer.getBuffer().toString());
call.execute();
call.close();
} finally {
session.getJdbcConnectionAccess().releaseConnection(connection);
}
} catch (Exception e) {
sLog.error("Unable to send request: "+e.getMessage(),e);
} finally {
_RootDAO.closeCurrentThreadSessions();
}
}
示例11: parseXml
import org.dom4j.Document; //导入依赖的package包/类
/**
* 解析微信发来的请求(XML)
*
* @param request
* @return Map<String, String>
* @throws Exception
*/
@SuppressWarnings("unchecked")
public static Map<String, String> parseXml(HttpServletRequest request) throws Exception {
// 将解析结果存储在HashMap中
Map<String, String> map = new HashMap<String, String>();
// 从request中取得输入流
InputStream inputStream = request.getInputStream();
// 读取输入流
SAXReader reader = new SAXReader();
Document document = reader.read(inputStream);
// 得到xml根元素
Element root = document.getRootElement();
// 得到根元素的所有子节点
List<Element> elementList = root.elements();
// 遍历所有子节点
for (Element e : elementList)
map.put(e.getName(), e.getText());
// 释放资源
inputStream.close();
inputStream = null;
return map;
}
示例12: MqResToDto
import org.dom4j.Document; //导入依赖的package包/类
/**
* 将mq查询结果包装成list--dto的形式,dto内容为item中的内容
*
* @param recv
* @return
*/
public static Map MqResToDto(String recv) {
// System.out.println("####recv"+recv);
List res = new ArrayList();
Map map = new HashMap();
try {
Document doc = DocumentHelper.parseText(recv);
List list = doc.selectNodes("//item");
Iterator<DefaultElement> it = list.iterator();
while (it.hasNext()) {
Map elementdto = XmlUtil.Dom2Map(it.next());
res.add(elementdto);
}
map.put("resultList", res);// 放入结果集
/*
* 如果存在REC_MNT,说明是分页查询类,需要将总记录数返回
*/
Node de = doc.selectSingleNode("//REC_MNT");
if (DataUtil.isNotEmpty(de)) {
map.put("countInteger", de.getText());
}
} catch (Exception e) {
log.error(XmlUtil.class, e);
}
return map;
}
示例13: loadResourceSecurityConfigs
import org.dom4j.Document; //导入依赖的package包/类
@Override
public List<UserPermission> loadResourceSecurityConfigs(String companyId) throws Exception{
List<UserPermission> configs=new ArrayList<UserPermission>();
String filePath=RESOURCE_SECURITY_CONFIG_FILE+(companyId == null ? "" : companyId);
Node rootNode=getRootNode();
Node fileNode = rootNode.getNode(filePath);
Property property = fileNode.getProperty(DATA);
Binary fileBinary = property.getBinary();
InputStream inputStream = fileBinary.getStream();
String content = IOUtils.toString(inputStream, "utf-8");
inputStream.close();
Document document = DocumentHelper.parseText(content);
Element rootElement = document.getRootElement();
for (Object obj : rootElement.elements()) {
if (!(obj instanceof Element)) {
continue;
}
Element element = (Element) obj;
if (!element.getName().equals("user-permission")) {
continue;
}
UserPermission userResource=new UserPermission();
userResource.setUsername(element.attributeValue("username"));
userResource.setProjectConfigs(parseProjectConfigs(element));
configs.add(userResource);
}
return configs;
}
示例14: Dom2Map
import org.dom4j.Document; //导入依赖的package包/类
/**
* 解析XML并将其节点元素压入Dto返回(基于节点值形式的XML格式) 应用于复杂对象
*
* @param pStrXml 待解析的XML字符串
* @return outDto 返回Dto
*/
public static Map Dom2Map(Document doc) {
Map map = new HashMap();
if (doc == null)
return map;
Element root = doc.getRootElement();
for (Iterator iterator = root.elementIterator(); iterator.hasNext();) {
Element e = (Element) iterator.next();
// System.out.println(e.getName());
List list = e.elements();
if (list.size() > 0) {
map.put(e.getName(), Dom2Map(e));
} else
map.put(e.getName(), e.getText());
}
return map;
}
示例15: parseDto2Xml
import org.dom4j.Document; //导入依赖的package包/类
/**
* 将Dto转换为符合XML标准规范格式的字符串(基于节点值形式)
*
* @param dto 传入的Dto对象
* @param pRootNodeName 根结点名
* @return string 返回XML格式字符串
*/
public static final String parseDto2Xml(Map map, String pRootNodeName) {
Document document = DocumentHelper.createDocument();
// 增加一个根元素节点
document.addElement(pRootNodeName);
Element root = document.getRootElement();
Iterator keyIterator = map.keySet().iterator();
while (keyIterator.hasNext()) {
String key = (String) keyIterator.next();
String value = (String) map.get(key);
Element leaf = root.addElement(key);
leaf.setText(value);
}
// 将XML的头声明信息截去
String outXml = document.asXML().substring(39);
return outXml;
}