您的位置:首页 > 其它

对xml格式的字符串的一些操作

2012-03-01 20:28 148 查看
/**
*  Dom解析XML格式的字符串
*/
package com.kotei.lbs.TaskSchedule;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

public class TaskLoadXMLFile {
String flag ;
String msg;
String id;
String sendtime;
String sendcontent;
String coordinate;
String sender;
String status;
String tasktype;
String remark;
String username;
String graphicld;
public boolean LoadFromFile(String path){
boolean successful = true;
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
try {
InputStream inStream = new  FileInputStream(new File(path));
DocumentBuilder builder = factory.newDocumentBuilder();
Document dom = builder.parse(inStream);
Element root = dom.getDocumentElement();

// read
NodeList items = root.getElementsByTagName("ReplyMsg");
// if get the item
if (items.getLength()>0) {
Element personNode = (Element) items.item(0);
NodeList childsNodes = personNode.getChildNodes();
for (int j = 0; j < childsNodes.getLength(); j++) {
Node node = (Node) childsNodes.item(j);
if(node.getNodeType() == Node.ELEMENT_NODE){
Element childNode = (Element) node;
String nodeName = childNode.getNodeName();
String nodeValue = childNode.getTextContent();
//判断是否name元素
if (nodeName.equalsIgnoreCase("ServerReplyMessage")) {
// if(nodeValue.length()>0)
// redfileFileName = nodeValue;
NodeList childsNodes1 = childNode.getChildNodes();
for (int i = 0; i < childsNodes1.getLength(); i++) {
Node node1 = (Node) childsNodes1.item(i);
if(node1.getNodeType() == Node.ELEMENT_NODE){
Element childNode1 = (Element) node1;
String nodeName1 = childNode1.getNodeName();
String nodeValue1 = childNode1.getTextContent();
if(nodeName1.equalsIgnoreCase("Flag")){
if(nodeValue1.length() > 0){
flag = nodeValue1;
}
}else if(nodeName1.equalsIgnoreCase("Msg")){
if(nodeValue1.length() > 0){
msg = nodeValue1;
}
}else if(nodeName1.equalsIgnoreCase("ShortMsgs")){
NodeList childsNodes2 = childNode1.getChildNodes();
for (int k = 0; k < childsNodes2.getLength(); k++) {
Node node2 = (Node) childsNodes2.item(k);
if(node2.getNodeType() == Node.ELEMENT_NODE){
Element childNode2 = (Element) node2;
String nodeName2 = childNode2.getNodeName();
String nodeValue2 = childNode2.getTextContent();
if(nodeName2.equalsIgnoreCase("ShortMsg")){
NodeList childsNodes3 = childNode2.getChildNodes();
for (int l = 0; l < childsNodes3.getLength(); l++) {
Node node3 = childsNodes3.item(l);
if(node3.getNodeType() == Node.ELEMENT_NODE){
Element childNode3 = (Element) node3;
String nodeName3 = childNode3.getNodeName();
String nodeValue3 = childNode3.getTextContent();
if(nodeName3.equalsIgnoreCase("ID")){
if(nodeValue3.length() > 0){
id = nodeValue3;
}
}else if(nodeName3.equalsIgnoreCase("SendTime")){
if(nodeValue3.length() > 0){
sendtime = nodeValue3;
}
}else if(nodeName3.equalsIgnoreCase("SendContent")){
if(nodeValue3.length() > 0){
sendcontent = nodeValue3;
}
}else if(nodeName3.equalsIgnoreCase("Coordinate")){
if(nodeValue3.length() > 0){
coordinate = nodeValue3;
}
}else if(nodeName3.equalsIgnoreCase("Sender")){
if(nodeValue3.length() > 0){
sender = nodeValue3;
}
}else if(nodeName3.equalsIgnoreCase("Status")){
if(nodeValue3.length() > 0){
status = nodeValue3;
}
}else if(nodeName3.equalsIgnoreCase("TaskType")){
if(nodeValue3.length() > 0){
tasktype = nodeValue3;
}
}else if(nodeName3.equalsIgnoreCase("REMARK")){
if(nodeValue3.length() > 0){
remark = nodeValue3;
}
}else if(nodeName3.equalsIgnoreCase("UserName")){
if(nodeValue3.length() > 0){
username = nodeValue3;
}
}else if(nodeName3.equalsIgnoreCase("GraphicId")){
if(nodeValue3.length() > 0){
graphicld = nodeValue3;
}
}
}
}
}
}
}
}
}
}
}
}
}
}
inStream.close();
}catch(IOException e){
successful = false;
e.printStackTrace();
}catch (Exception e) {
successful = false;
e.printStackTrace();
}
return successful;
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: