是这样的,我只能用流读取txt中的字符串,有人会问为什么不直接保存到xml中去获取节点里的值呢,这是需求我没办法改变,具体需求是读取txt中的内容加载到DOM空间然后获取节点里的值,只需要SystemName,SystemPassword,endpointURL,CheckTime,Mode这几个节点里的值,我想请教下大家怎么获取,很急的,谢谢了txt文本内容如下:
<?xml version="1.0" encoding="utf-8"?>
<CollStations>
  <CollStation>
    <SystemName>ismgA0(ismg-agent)</SystemName>
    <SystemPassword>123456</SystemPassword>
    <endpointURL>http://10.25.116.242:8080/axis/SendDataBySoap_java.jws?wsdl</endpointURL>
    <CheckTime>5000</CheckTime>
    <Mode>RunMode</Mode>
    <Type>descrypt</Type>
    <Scripts>
      <ScriptType>Connect</ScriptType>
      <CommandID>0x4e000100</CommandID>
      <Ip>10.26.50.123</Ip>
      <Port>22</Port>
      <User>xunjian</User>
      <Password>zyjkWG!@</Password>
      <RtnChar>xunjian></RtnChar>
      <CommandPrompt> </CommandPrompt>
      <TimeOut>15000</TimeOut>
    </Scripts>
    <Scripts>
      <ScriptType>send</ScriptType>
      <CommandID>0x4e000200</CommandID>
      <CommandString>ssh [email protected]</CommandString>
      <CommandPrompt>:</CommandPrompt>
      <ContinuePrompt> </ContinuePrompt>
      <ContinueString> </ContinueString>
      <StopString> </StopString>
      <TimeOut>15000</TimeOut>
    </Scripts>
    <Scripts>
      <ScriptType>send</ScriptType>
      <CommandID>0x4e000400</CommandID>
      <CommandString>zyjkWG!@</CommandString>
      <CommandPrompt>$</CommandPrompt>
      <ContinuePrompt> </ContinuePrompt>
      <ContinueString> </ContinueString>
      <StopString> </StopString>
      <TimeOut>15000</TimeOut>
    </Scripts>
    <Scripts>
      <ScriptType>send</ScriptType>
      <CommandID>0x00000300</CommandID>
      <CommandString>ntpq -p</CommandString>
      <CommandPrompt>$</CommandPrompt>
      <ContinuePrompt> </ContinuePrompt>
      <ContinueString> </ContinueString>
      <StopString> </StopString>
      <TimeOut>15000</TimeOut>
    </Scripts>
    <Scripts>
      <ScriptType>send</ScriptType>
      <CommandID>0x00000200</CommandID>
      <CommandString>date '+%Y-%m-%d %H:%M:%S'</CommandString>
      <CommandPrompt>$</CommandPrompt>
      <ContinuePrompt> </ContinuePrompt>
      <ContinueString> </ContinueString>
      <StopString> </StopString>
      <TimeOut>15000</TimeOut>
    </Scripts>
    <Scripts>
      <ScriptType>send</ScriptType>
      <CommandID>0x4e061000</CommandID>
      <CommandString>exit</CommandString>
      <CommandPrompt>xunjian></CommandPrompt>
      <ContinuePrompt> </ContinuePrompt>
      <ContinueString> </ContinueString>
      <StopString> </StopString>
      <TimeOut>15000</TimeOut>
    </Scripts>
  </CollStation>
  <CollStation>
    <SystemName>ismgA0(ismg-agent)</SystemName>
    <SystemPassword>123456</SystemPassword>
    <endpointURL>http://10.25.116.242:8080/axis/SendDataBySoap_java.jws?wsdl</endpointURL>
    <CheckTime>5000</CheckTime>
    <Mode>RunMode</Mode>
    <Type>descrypt</Type>
    <Scripts>
      <ScriptType>Connect</ScriptType>
      <CommandID>0x4e000100</CommandID>
      <Ip>10.26.50.123</Ip>
      <Port>22</Port>
      <User>xunjian</User>
      <Password>zyjkWG!@</Password>
      <RtnChar>xunjian></RtnChar>
      <CommandPrompt> </CommandPrompt>
      <TimeOut>15000</TimeOut>
    </Scripts>
    <Scripts>
      <ScriptType>send</ScriptType>
      <CommandID>0x4e000200</CommandID>
      <CommandString>ssh [email protected]</CommandString>
      <CommandPrompt>:</CommandPrompt>
      <ContinuePrompt> </ContinuePrompt>
      <ContinueString> </ContinueString>
      <StopString> </StopString>
      <TimeOut>15000</TimeOut>
    </Scripts>
    <Scripts>
      <ScriptType>send</ScriptType>
      <CommandID>0x4e000400</CommandID>
      <CommandString>zyjkWG!@</CommandString>
      <CommandPrompt>$</CommandPrompt>
      <ContinuePrompt> </ContinuePrompt>
      <ContinueString> </ContinueString>
      <StopString> </StopString>
      <TimeOut>15000</TimeOut>
    </Scripts>
    <Scripts>
      <ScriptType>send</ScriptType>
      <CommandID>0x00000300</CommandID>
      <CommandString>ntpq -p</CommandString>
      <CommandPrompt>$</CommandPrompt>
      <ContinuePrompt> </ContinuePrompt>
      <ContinueString> </ContinueString>
      <StopString> </StopString>
      <TimeOut>15000</TimeOut>
    </Scripts>
    <Scripts>
      <ScriptType>send</ScriptType>
      <CommandID>0x00000200</CommandID>
      <CommandString>date '+%Y-%m-%d %H:%M:%S'</CommandString>
      <CommandPrompt>$</CommandPrompt>
      <ContinuePrompt> </ContinuePrompt>
      <ContinueString> </ContinueString>
      <StopString> </StopString>
      <TimeOut>15000</TimeOut>
    </Scripts>
    <Scripts>
      <ScriptType>send</ScriptType>
      <CommandID>0x4e061000</CommandID>
      <CommandString>exit</CommandString>
      <CommandPrompt>xunjian></CommandPrompt>
      <ContinuePrompt> </ContinuePrompt>
      <ContinueString> </ContinueString>
      <StopString> </StopString>
      <TimeOut>15000</TimeOut>
    </Scripts>
  </CollStation>
  <CollStation>
    <SystemName>ismgA0(ismg-agent)</SystemName>
    <SystemPassword>123456</SystemPassword>
    <endpointURL>http://10.25.116.242:8080/axis/SendDataBySoap_java.jws?wsdl</endpointURL>
    <CheckTime>5000</CheckTime>
    <Mode>RunMode</Mode>
    <Type>descrypt</Type>
    <Scripts>
      <ScriptType>Connect</ScriptType>
      <CommandID>0x4e000100</CommandID>
      <Ip>10.26.50.123</Ip>
      <Port>22</Port>
      <User>xunjian</User>
      <Password>zyjkWG!@</Password>
      <RtnChar>xunjian></RtnChar>
      <CommandPrompt> </CommandPrompt>
      <TimeOut>15000</TimeOut>
    </Scripts>
    <Scripts>
      <ScriptType>send</ScriptType>
      <CommandID>0x4e000200</CommandID>
      <CommandString>ssh [email protected]</CommandString>
      <CommandPrompt>:</CommandPrompt>
      <ContinuePrompt> </ContinuePrompt>
      <ContinueString> </ContinueString>
      <StopString> </StopString>
      <TimeOut>15000</TimeOut>
    </Scripts>
    <Scripts>
      <ScriptType>send</ScriptType>
      <CommandID>0x4e000400</CommandID>
      <CommandString>zyjkWG!@</CommandString>
      <CommandPrompt>$</CommandPrompt>
      <ContinuePrompt> </ContinuePrompt>
      <ContinueString> </ContinueString>
      <StopString> </StopString>
      <TimeOut>15000</TimeOut>
    </Scripts>
    <Scripts>
      <ScriptType>send</ScriptType>
      <CommandID>0x00000300</CommandID>
      <CommandString>ntpq -p</CommandString>
      <CommandPrompt>$</CommandPrompt>
      <ContinuePrompt> </ContinuePrompt>
      <ContinueString> </ContinueString>
      <StopString> </StopString>
      <TimeOut>15000</TimeOut>
    </Scripts>
    <Scripts>
      <ScriptType>send</ScriptType>
      <CommandID>0x00000200</CommandID>
      <CommandString>date '+%Y-%m-%d %H:%M:%S'</CommandString>
      <CommandPrompt>$</CommandPrompt>
      <ContinuePrompt> </ContinuePrompt>
      <ContinueString> </ContinueString>
      <StopString> </StopString>
      <TimeOut>15000</TimeOut>
    </Scripts>
    <Scripts>
      <ScriptType>send</ScriptType>
      <CommandID>0x4e061000</CommandID>
      <CommandString>exit</CommandString>
      <CommandPrompt>xunjian></CommandPrompt>
      <ContinuePrompt> </ContinuePrompt>
      <ContinueString> </ContinueString>
      <StopString> </StopString>
      <TimeOut>15000</TimeOut>
    </Scripts>
  </CollStation>
  <CollStation>
    <SystemName>ismgA0(ismg-agent)</SystemName>
    <SystemPassword>123456</SystemPassword>
    <endpointURL>http://10.25.116.242:8080/axis/SendDataBySoap_java.jws?wsdl</endpointURL>
    <CheckTime>5000</CheckTime>
    <Mode>RunMode</Mode>
    <Type>descrypt</Type>
    <Scripts>
      <ScriptType>Connect</ScriptType>
      <CommandID>0x4e000100</CommandID>
      <Ip>10.26.50.123</Ip>
      <Port>22</Port>
      <User>xunjian</User>
      <Password>zyjkWG!@</Password>
      <RtnChar>xunjian></RtnChar>
      <CommandPrompt> </CommandPrompt>
      <TimeOut>15000</TimeOut>
    </Scripts>
    <Scripts>
      <ScriptType>send</ScriptType>
      <CommandID>0x4e000200</CommandID>
      <CommandString>ssh [email protected]</CommandString>
      <CommandPrompt>:</CommandPrompt>
      <ContinuePrompt> </ContinuePrompt>
      <ContinueString> </ContinueString>
      <StopString> </StopString>
      <TimeOut>15000</TimeOut>
    </Scripts>
    <Scripts>
      <ScriptType>send</ScriptType>
      <CommandID>0x4e000400</CommandID>
      <CommandString>zyjkWG!@</CommandString>
      <CommandPrompt>$</CommandPrompt>
      <ContinuePrompt> </ContinuePrompt>
      <ContinueString> </ContinueString>
      <StopString> </StopString>
      <TimeOut>15000</TimeOut>
    </Scripts>
    <Scripts>
      <ScriptType>send</ScriptType>
      <CommandID>0x00000300</CommandID>
      <CommandString>ntpq -p</CommandString>
      <CommandPrompt>$</CommandPrompt>
      <ContinuePrompt> </ContinuePrompt>
      <ContinueString> </ContinueString>
      <StopString> </StopString>
      <TimeOut>15000</TimeOut>
    </Scripts>
    <Scripts>
      <ScriptType>send</ScriptType>
      <CommandID>0x00000200</CommandID>
      <CommandString>date '+%Y-%m-%d %H:%M:%S'</CommandString>
      <CommandPrompt>$</CommandPrompt>
      <ContinuePrompt> </ContinuePrompt>
      <ContinueString> </ContinueString>
      <StopString> </StopString>
      <TimeOut>15000</TimeOut>
    </Scripts>
    <Scripts>
      <ScriptType>send</ScriptType>
      <CommandID>0x4e061000</CommandID>
      <CommandString>exit</CommandString>
      <CommandPrompt>xunjian></CommandPrompt>
      <ContinuePrompt> </ContinuePrompt>
      <ContinueString> </ContinueString>
      <StopString> </StopString>
      <TimeOut>15000</TimeOut>
    </Scripts>
  </CollStation>
             </CollStations>

解决方案 »

  1.   

    String[] strArr = new String[4];   
      //特定的xml格式字符串   
      String str = "<ReturnValue><state>状态值</state><CommandID>CommandID</CommandID>" +   
      "<MsgSubSeq>MsgSubSeq</MsgSubSeq><Description>描述</Description>" +   
      "</ReturnValue>";   
      String state = str.substring(str.indexOf("<state>")+7,str.indexOf("</state>"));   
      String commandID = str.substring(str.indexOf("<CommandID>")+11,str.indexOf("</CommandID>"));   
      String msgSubSeq = str.substring(str.indexOf("<MsgSubSeq>")+11,str.indexOf("</MsgSubSeq>"));   
      String description = str.substring(str.indexOf("<Description>")+13,str.indexOf("</Description>"));  
      strArr[0] = state;   
      strArr[1] = commandID;   
      strArr[2] = msgSubSeq;   
      strArr[3] = description;
      

  2.   

    import java.util.Iterator;import org.dom4j.Document;
    import org.dom4j.Element;
    import org.dom4j.io.SAXReader;public class Test
    {
    public static void main(String[] args) throws Exception
    {
    SAXReader sr = new SAXReader(); Document d = sr.read("Untitled1.xml"); Element e = d.getRootElement().element("CollStation");
    for (Iterator iter = e.elementIterator(); iter.hasNext();)
    {
    Element ee = (Element) iter.next(); if ("SystemName".equals(ee.getName()))
    {
    System.out.println(ee.getText());
    }
    else if ("SystemPassword".equals(ee.getName()))
    {
    System.out.println(ee.getText());
    }
    else if ("endpointURL".equals(ee.getName()))
    {
    System.out.println(ee.getText());
    }
    else if ("CheckTime".equals(ee.getName()))
    {
    System.out.println(ee.getText());
    }
    else if ("Mode".equals(ee.getName()))
    {
    System.out.println(ee.getText());
    } } }
    }
      

  3.   


    你连需求都没看清,你这是直接读取XML,我说是用流读TXT的文本
      

  4.   

    xstream,直接序列化反序列化处理