Java--解析xml文档

须要解析的文档以下java

<?xml version="1.0"?>
<UnifiedContentDefine>
	<Head>
		<TaskGUID>c7104f47651045519917160355313d77</TaskGUID>
	</Head>
	<ContentInfo>
		<ContentID>c7104f47651045519917160355313d77</ContentID>
		<ContentData>
			<ContentFile>
				<FileItem>
					<FileTypeID>FILETYPE_HIGHMP4(写死)</FileTypeID>
					<FileName>
						<FullPath>a3_2016-11-3016_38_32.avi(素材名称,xml文件和素材在同一目录)</FullPath>
					</FileName>
				</FileItem>
			</ContentFile>
			<EntityData>
				<AttributeItem>
					<ItemCode>ClipName</ItemCode>
					<ItemName>素材名称</ItemName>
					<Value>a3</Value>
				</AttributeItem>
				<AttributeItem>
					<ItemCode>CreatorID</ItemCode>
					<ItemName>建立人ID</ItemName>
					<Value>0</Value>
				</AttributeItem>
				<AttributeItem>
					<ItemCode>CreatorName</ItemCode>
					<ItemName>建立人Name</ItemName>
					<Value>li</Value>
				</AttributeItem>
				<AttributeItem>
					<ItemCode>CreatorCode</ItemCode>
					<ItemName>建立人编码</ItemName>
					<Value>c7104f47651045519917160355313d77</Value>
				</AttributeItem>
				<AttributeItem>
					<ItemCode>CreateDate</ItemCode>
					<ItemName>建立时间</ItemName>
					<Value>42704.693426</Value>
				</AttributeItem>
				<AttributeItem>
					<ItemCode>ColumnName</ItemCode>
					<ItemName>栏目名称</ItemName>
					<Value>测试栏目</Value>
				</AttributeItem>
				<AttributeItem>
					<ItemCode>ColumnCode</ItemCode>
					<ItemName>栏目编码</ItemName>
					<Value>738197505</Value>
				</AttributeItem>
			</EntityData>
		</ContentData>
	</ContentInfo>
</UnifiedContentDefine>

代码以下node

//1.解析xml,abfilePath是xml文件的完整路径,其实是去读取该文件中的内容,保存为字符串
            String content = FileUtil.readText(abfilePath);
            if(StringUtil.isEmpty(content)){
                LogUtil.info("读取文件内容为空!");
                return;
            }
            //将文件内容转换为xml对象
            Document document = DocumentHelper.parseText(content);
            //获取根元素
            Element root = document.getRootElement();
            //Head
            Element head = root.element("Head");
            //TaskGUID
            Element taskGUID = head.element("TaskGUID");
            //取TaskGUID值
            String taskGUIDValue = taskGUID.getStringValue();

            //ContentInfo
            Element contentInfo = root.element("ContentInfo");
            //ContentID
            Element contentID = contentInfo.element("ContentID");
            //取ContentID值
            String contentIDValue = contentID.getStringValue();
            //ContentData
            Element contentData = contentInfo.element("ContentData");
            //ContentFile
            Element contentFile = contentData.element("ContentFile");
            //FileItem
            Element fileItem = contentFile.element("FileItem");
            //FileTypeID
            Element fileTypeID = fileItem.element("FileTypeID");
            //取FileTypeID值
            String fileTypeIDValue = fileTypeID.getStringValue();
            //FileName
            Element fileName = fileItem.element("FileName");
            //FullPath
            Element fullPath = fileName.element("FullPath");
            //取FullPath值
            String fullPathValue = fullPath.getStringValue();
            //EntityData
            Element entityData = contentData.element("EntityData");
            //AttributeItem
            List nodeList = entityData.selectNodes("AttributeItem");
            //ListObject
            List<JSONObject> jsonObjectList = new ArrayList<>();
            for (int i=0;i< nodeList.size();i++){
                Node mediaFile = (Node) nodeList.get(i);
                String itemCode = mediaFile.selectSingleNode("ItemCode").getText();
                String itemName = mediaFile.selectSingleNode("ItemName").getText();
                String value = mediaFile.selectSingleNode("Value").getText();
                JSONObject jsonObject = new JSONObject();
                jsonObject.put("itemCode",itemCode);
                jsonObject.put("itemName",itemName);
                jsonObject.put("value",value);
                //装入list
                jsonObjectList.add(jsonObject);
            }
            //保存解析的数据
            JSONObject objectIn = new JSONObject();
            objectIn.put("taskGUIDValue",taskGUIDValue);
            objectIn.put("contentIDValue",contentIDValue);
            objectIn.put("fileTypeIDValue",fileTypeIDValue);
            objectIn.put("fullPathValue",fullPathValue);
            objectIn.put("jsonObjectList",jsonObjectList);

FileUtil.javajson

public class FileUtil {

	public static String normalizePath(String path) {
		path = path.replace('\\', '/');
		path = StringUtil.replaceEx(path, "../", "/");
		path = StringUtil.replaceEx(path, "./", "/");
		if (path.endsWith("..")) {
			path = path.substring(0, path.length() - 2);
		}
		path = path.replaceAll("/+", "/");
		return path;
	}
	public static String readText(String fileName) {
		fileName = normalizePath(fileName);
		return readText(fileName, Constant.GlobalCharset);
	}
	public static String readText(String fileName, String encoding) {
		fileName = normalizePath(fileName);
		try {
			InputStream is = new FileInputStream(fileName);
			String str = readText(is, encoding);
			is.close();
			return str;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
}
相关文章
相关标签/搜索