本文整理匯總了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;
}