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;
	}
}
相關文章
相關標籤/搜索