zoukankan      html  css  js  c++  java
  • 对xml格式的字符串的一些操作

    /**
    * 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;
    }
    }


     

  • 相关阅读:
    ES权威指南1
    JS易错知识点
    配置中心
    Elasticsearch 2.3.2 从oracle中同步数据
    Logstash同步Oracle数据到ElasticSearch
    从Oracle到Elasticsearch
    JS实现音乐播放器
    压力测试
    spring boot + quartz 集群
    用户体验很好的密码校验js
  • 原文地址:https://www.cnblogs.com/jh5240/p/2376170.html
Copyright © 2011-2022 走看看