对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; } }
相关文章推荐
- PHP中一些可以替代正则表达式函数的字符串操作函数
- 一些常用字符串操作函数的内部实现
- C#中一些字符串操作的常用用法
- 实现一些字符串操作标准库函数、解决一些字符串问题
- 一些操作字符串的方法
- ios中字符串的一些常见操作
- 字符串的一些操作(增删改查)
- 一些关于字符串的操作
- perl里字符串取长度,比较等一些操作
- 操作xml格式的字符串的方法(C#)
- 将后台返回字符串数据转为jquery对象,并做一些操作
- C#中一些字符串操作的常用用法
- 字符串的一些操作
- VB编程的一些小技巧 - 关于vb字符串操作的优化
- C语言中操作字符串的一些函数源代码[转]
- C#中一些字符串操作的常用用法,c#编码和解码
- C#中一些字符串操作的常用用法
- C语言中操作字符串的一些函数源代码
- perl里字符串取长度,比较等一些操作
- C#中一些字符串操作的常用用法