×

javadom4j

javadom4j的简单介绍

admin admin 发表于2023-04-13 17:58:07 浏览79 评论0

抢沙发发表评论

本文目录一览:

在java中怎么用dom4j解析XML文件??

以下是曾经写的一丛搏个解析XML获取XML中图片流的字符串,获取并转化为图片的工具类

里面海带哟base64编码,具体代码如下,希望能帮到你

package com.asidel.web.util;

import java.io.File;

import java.io.FileOutputStream;

import java.io.OutputStream;

import java.util.Iterator;

import java.util.List;

import org.dom4j.Document;

import org.dom4j.DocumentHelper;

import org.dom4j.Element;

import sun.misc.BASE64Decoder;

import sun.misc.BASE64Encoder;

import com.dragonsoft.adapter.AdapterSend;

public class XMLparserUtil {

/**

 * @Title: getXp

 * @Description: 解析XML

 * @return 

 */

public static String getXp(String xmlStr,String sfzh) {

String xp = "";

try {

// 将String转化成xml

Document document = DocumentHelper.parseText(xmlStr);

// 获取Value节点下的Row子节点

List RowElementList = XMLparserUtil.getRowElementList(document);

if 羡早(RowElementList != null  RowElementList.size()  0) {

Element RowElement3 = (Element) RowElementList.get(RowElementList.size() - 1);

// 根据节点获取值

String xpBase64 = RowElement3.elementTextTrim("Data");

// System.out.println("xpBase64:" + xpBase64);

if (!"".equals(xpBase64)  xpBase64 != null) {

xp = XMLparserUtil.getDecodingBASE64(xpBase64, sfzh);

}

// System.out.println("xp:" 渗派祥+ xp);

}

} catch (Exception e) {

// TODO Auto-generated catch block

e.printStackTrace();

xp = "";

}

return xp;

}

/**

 * @Title: getEncodingBASE64

 * @Description: 进行 BASE64 编码

 * @param string

 * @return 

 */

public static String getEncodingBASE64(String string) {

String returnStr = "";

if (!"".equals(string)  string != null) {

try {

BASE64Encoder base64Encoder = new BASE64Encoder();

returnStr = base64Encoder.encode(string.getBytes());

} catch (Exception e) {

// TODO: handle exception

e.printStackTrace();

}

}

return returnStr;

}

/**

 * @Title: getDecodingBASE64

 * @Description: 将 BASE64 编码的字符串进行解码

 * @param string

 * @return 

 */

public static String getDecodingBASE64(String imgStr, String imgName) {

String imgFilePath = "";// 新生成的图片

BASE64Decoder decoder = new BASE64Decoder();

if (!"".equals(imgStr)  imgStr != null) {

try {

byte[] b = decoder.decodeBuffer(imgStr);

for (int i = 0; i  b.length; ++i) {

if (b[i]  0) {// 调整异常数据

b[i] += 256;

}

}

// 生成jpeg图片

PathUtil pathUtil = new PathUtil();

String zdryxppath = pathUtil.getWebRoot()+"uploadImages/";

System.out.println("zdryxppath="+zdryxppath);

File pathDir = new File(zdryxppath);// 如果目录不存在就创建该目录

if (!pathDir.exists()) {

pathDir.mkdirs();

}

String imgFileRealPath = zdryxppath + imgName + ".jpg";// 新生成的图片

OutputStream out = new FileOutputStream(imgFileRealPath);

out.write(b);

out.flush();

out.close();

imgFilePath = "uploadImages/" + imgName + ".jpg";

} catch (Exception e) {

e.printStackTrace();

}

}

return imgFilePath;

}

/**

 * @Title: getRowElementList

 * @Description: 获取节点列表

 * @param document

 * @param string

 * @return 

 * 

 */

public static List getRowElementList(Document document) {

List returnRowElementList = null;

// 获取根节点

Element rootElement = document.getRootElement();

// System.out.println("rootElement:"+rootElement);

// 获取根节点下的Method子节点

Iterator MethodElementList = rootElement.elementIterator("Method");

// 遍历Method节点

while (MethodElementList.hasNext()) {

Element MethodElement = (Element) MethodElementList.next();

// System.out.println("Method:"+MethodElement);

// 获取Method节点下的Items子节点

Iterator ItemsElementList = MethodElement.elementIterator("Items");

// 遍历Items节点

while (ItemsElementList.hasNext()) {

Element ItemsElement = (Element) ItemsElementList.next();

// System.out.println("Items:"+ItemsElement);

// 获取Items节点下的Item子节点

Iterator ItemElementList = ItemsElement.elementIterator("Item");

// 遍历Item节点

while (ItemElementList.hasNext()) {

Element ItemElement = (Element) ItemElementList.next();

// System.out.println("Item:"+ItemElement);

// 获取Item节点下的Value子节点

Iterator ValueElementList = ItemElement.elementIterator("Value");

// 遍历Value节点

while (ValueElementList.hasNext()) {

Element ValueElement = (Element) ValueElementList.next();

// System.out.println("Value:"+ValueElement);

returnRowElementList = ValueElement.elements("Row");

// // 获取Value节点下的Row子节点

// List

// if (RowElementList != null  RowElementList.size() 

// 0) {

// Element RowElement3 = (Element)

// RowElementList.get(RowElementList.size() - 1);

// // System.out.println("Row:"+RowElement3);

// // 根据节点获取值

// xp = RowElement3.elementTextTrim("Data");

// System.out.println("xp:" + xp);

// }

}

}

}

}

return returnRowElementList;

}

public static void main(String[] args) {}

}

java中用dom4j如何遍历循环XML各个节点,将树状结构输出到控制台??请帮忙写出代码,谢谢!

import java.io.File;import org.dom4j.Document;import org.dom4j.DocumentException;import org.dom4j.Element;import org.dom4j.io.SAXReader;public class TestXML { public static void main(String[] args) { SAXReader reader = new SAXReader(); try { Document document = reader.read(new File("test.xml")); Element root = document.getRootElement(); listNodes(root, ""); } catch (DocumentException e) { e.printStackTrace(); } } private static void listNodes(Element node, String prefix) { System.out.println(prefix + node.getName()); for (int i = 0, size = node.nodeCount(); i size; i++) { org.dom4j.Node n = node.node(i); if (n instanceof Element) { listNodes((Element) n, prefix + " "); } } }}-javadom4j

java中dom4j解析xml文件怎么获取节点属性

dom4j中山者,使用Element.attributes方法可以获取到节点的属性,而使用elements则可以获取相应的子节点

比如:

Element root = doc.getRootElement();

List attrList = root.attributes();

for (int i = 0; i 芦薯 attrList.size(); i++) {

//属性的取得

Attribute item = (Attribute)attrList.get(i);

System.out.println(item.getName() + "逗哗薯=" + item.getValue());

}

List childList = root.elements();

for (int i = 0; i childList.size(); i++) {

//子节点的操作

Element it = (Element) childList.get(i);

//对子节点进行其它操作...

}