如何从结构类似于Java中XML的文件中获取特定元素

如何从结构类似于Java中XML的文件中获取特定元素,java,xml,file,substring,Java,Xml,File,Substring,我有一个.sic文件,其结构类似于XML,但并不完全相同。在那里我有一个章节Channel2,我想在那里读一些元素。这一部分是这样的: . . . <SI name = "Channel2" type = "list"> <SI name = "SecsPortConfig" type = "list"> <SI name = "PortType" type = "string">'XXX'</SI>

我有一个.sic文件,其结构类似于XML,但并不完全相同。在那里我有一个章节
Channel2
,我想在那里读一些元素。这一部分是这样的:

.
.
.
<SI name = "Channel2" type = "list">
           <SI name = "SecsPortConfig" type = "list">
              <SI name = "PortType" type = "string">'XXX'</SI>
              <SI name = "Protocol" type = "string">'XXX'</SI>
              <SI name = "Serial" type = "list">
                 <SI name = "Port" type = "int">'XXX'</SI>
                 <SI name = "Speed" type = "int">'XXXX'</SI>
              </SI>
              <SI name = "Socket" type = "list">
                 <SI name = "ConnectionMode" type = "string">'XXX'</SI>
                 <SI name = "LocalHost" type = "string">'XXX.XXX.XXX.XXX'</SI>
                 <SI name = "LocalPort" type = "int">'XXX'</SI>
                 <SI name = "RemoteHost" type = "string">'XXX.XXX.XXX'</SI>
                 <SI name = "RemotePort" type = "int">'XXX'</SI>
              </SI>
              <SI name = "HSMS" type = "list">
                 <SI name = "T5" type = "int">'XXX'</SI>
                 <SI name = "T6" type = "int">'XXX'</SI>
                 <SI name = "T7" type = "int">'XXX'</SI>
                 <SI name = "T8" type = "int">'XXX'</SI>
                 <SI name = "LinkTestTime" type = "int">'XXX'</SI>
              </SI>
              <SI name = "SECSI" type = "list">
                 <SI name = "T1" type = "int">'XXX'</SI>
                 <SI name = "T2" type = "int">'XXX'</SI>
                 <SI name = "T4" type = "int">'XXX'</SI>
                 <SI name = "RTY" type = "int">'XXX'</SI>
                 <SI name = "IsHost" type = "bool">'XXX'</SI>
                 <SI name = "IsMaster" type = "bool">'XXX'</SI>
                 <SI name = "InterleaveBlocks" type = "bool">'XXX'</SI>
              </SI>
              <SI name = "SECSII" type = "list">
                 <SI name = "DeviceID" type = "int">'XXX'</SI>
                 <SI name = "T3" type = "int">'XXX'</SI>
                 <SI name = "MultipleOpen" type = "bool">'XXX'</SI>
                 <SI name = "AutoDeviceID" type = "bool">'XXX'</SI>
              </SI>
              <SI name = "Log" type = "list">
                 <SI name = "LogCharError" type = "bool">'XXX'</SI>
                 <SI name = "LogCharEvent" type = "bool">'XXX'</SI>
                 <SI name = "LogCharReceive" type = "bool">'XXX'</SI>
                 <SI name = "LogCharSend" type = "bool">'XXX'</SI>
                 <SI name = "LogSecsIHsmsError" type = "bool">'XXX'</SI>
                 <SI name = "LogSecsIHsmsEvent" type = "bool">'XXX'</SI>
                 <SI name = "LogSecsIHsmsReceive" type = "bool">'XXX'</SI>
                 <SI name = "LogSecsIHsmsSend" type = "bool">'XXX'</SI>
                 <SI name = "LogSecsIIError" type = "bool">'XXX'</SI>
                 <SI name = "LogSecsIIEvent" type = "bool">'XXX'</SI>
                 <SI name = "LogSecsIIReceive" type = "bool">'XXX'</SI>
                 <SI name = "LogSecsIISend" type = "bool">'XXX'</SI>
              </SI>
           </SI>
           <SI name = "UseSeparateSECSLogFile" type = "bool">'XXX'</SI>
           <SI name = "Connected" type = "bool">'XXX'</SI>
           <SI name = "MessageFilters" type = "list">
              <SI name = "DeviceIDList" type = "list"/>
              <SI name = "StreamFunctionList" type = "list"/>
           </SI>
           <SI name = "SafeMessageFilters" type = "list">
              <SI name = "DeviceIDList" type = "list"/>
              <SI name = "StreamFunctionList" type = "list"/>
           </SI>
        </SI>
        .
        .
        .
Document\u myDoc=null;
LSInput input=implLS.createLSInput();
input.setStringData(requestXML);
_myDoc=parser.parse(输入);
SI=((NodeList)_myDoc.getElementsByTagName(“MessageFilters”)).item(0.getFirstChild().getNodeValue();

可以使用getElementsByTagName获取XML元素的节点值。但对于这一点,您需要有不同的元素名称。这不是答案。这不是答案。只是一个提示。试试这个。

因为我们不知道整个文件是如何形成的:
即使它不是完整的XML文档,也可以从文件的其余部分提取XML片段,并通过添加根元素将其转换为格式良好的XML文档

之后,您可以将其解析为文档,并使用XPath提取所需信息

下面是一些可以为您工作的Java代码示例(为了清楚起见,我没有包括xml)

import org.w3c.dom.Document;
导入org.xml.sax.InputSource;
导入org.xml.sax.SAXException;
导入javax.xml.parsers.DocumentBuilder;
导入javax.xml.parsers.DocumentBuilderFactory;
导入javax.xml.parsers.parserConfiguration异常;
导入javax.xml.transform.TransformerException;
导入javax.xml.xpath.xpath;
导入javax.xml.xpath.XPathConstants;
导入javax.xml.xpath.XPathExpressionException;
导入javax.xml.xpath.XPathFactory;
导入java.io.IOException;
导入java.io.StringReader;
公共类转换XML{
公共静态void main(字符串[]args)抛出ParserConfiguration异常、IOException、SAXException、TransformerException、XPathExpressionException{
//类似XML的内容
String xmlString=“xml here”;
//使用根元素将xml片段转换为格式良好的xml
字符串xmlStringWellformed=“”+xmlString+”;
//解析格式良好的xml
DocumentBuilderFactory工厂=DocumentBuilderFactory.newInstance();
DocumentBuilder=factory.newDocumentBuilder();
documentdocument=builder.parse(新的InputSource(新的StringReader(xmlStringWellformed));
//构建xpath表达式
字符串xPathRemoteHost=“//SI[@name='Channel2']/SI[@name='SecsPortConfig']/SI[@name='Socket']/SI[@name='RemoteHost']]/text()”;
字符串xPathRemotePort=“//SI[@name='Channel2']/SI[@name='SecsPortConfig']/SI[@name='Socket']/SI[@name='RemotePort']]/text()”;
XPath=XPathFactory.newInstance().newXPath();
//使用XPath进行提取
String remoteHost=(String)xPath.compile(xPathRemoteHost).evaluate(文档,XPathConstants.String);
字符串remotePort=(字符串)xPath.compile(xPathRemotePort).evaluate(文档,XPathConstants.String);
System.out.println(“远程主机:“+RemoteHost”);
System.out.println(“远程端口:“+RemotePort”);
}
}
资料来源:

“如何处理此类文件?”-就像处理其他xml文件一样。您是否研究过解析此文件以及如何使用xpath语句查找元素?也许我是瞎了。。。但是,为什么这不是XML文件呢?您向我们展示的部分似乎是格式良好的XML。我搞不懂这个问题是关于什么的。它是以xml形式形成的,但它们不像普通xml那样被标记。当我使用DOM解析器时,应该如何获取元素?我可以解析它,但是像getElementsByTagName这样的方法无法读取该元素。普通的xml文件如下所示:“Lokesh Gupta India..”我想读的文件总是以SI名称开头,所以我不知道如何访问元素。谢谢你的回答!我切换到python并解决了这个问题,但我必须用jython在java中实现python代码。有了它,我就可以不用使用python来完成。我现在试过了,效果非常好:)
    File file = new File("C:\\Users\\but\\Desktop\\ExternalswPassThroughSrv.sic");

    int counter = 0;

    BufferedReader br = new BufferedReader(new FileReader(file));

    String cl;
    String finalString = "";
    while ((cl = br.readLine()) != null) {
        if (cl.contains("Channel2")) {
            counter = 63;
        }
        if(counter != 0){
            //System.out.println(cl);
            finalString += cl + "\n";
            counter--;
        }
    }
    System.out.println(finalString);
import org.w3c.dom.Document;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.TransformerException;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathExpressionException;
import javax.xml.xpath.XPathFactory;
import java.io.IOException;
import java.io.StringReader;

public class ConvertXml {
    public static void main(String[] args) throws ParserConfigurationException, IOException, SAXException, TransformerException, XPathExpressionException {
        // Your XML-like content
        String xmlString = "xml here";

        // transform xml-Fragment into well-formed xml with root element
        String xmlStringWellformed = "<content>" + xmlString + "</content>";

        // parse well-formed xml
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        DocumentBuilder builder = factory.newDocumentBuilder();
        Document document = builder.parse(new InputSource(new StringReader(xmlStringWellformed)));

        // build xpath expression
        String xPathRemoteHost = "//SI[@name='Channel2']/SI[@name='SecsPortConfig']/SI[@name='Socket']/SI[@name='RemoteHost']/text()";
        String xPathRemotePort = "//SI[@name='Channel2']/SI[@name='SecsPortConfig']/SI[@name='Socket']/SI[@name='RemotePort']/text()";
        XPath xPath = XPathFactory.newInstance().newXPath();

        // Use XPath for extraction
        String remoteHost = (String) xPath.compile(xPathRemoteHost).evaluate(document, XPathConstants.STRING);
        String remotePort = (String) xPath.compile(xPathRemotePort).evaluate(document, XPathConstants.STRING);

        System.out.println("RemoteHost: " + remoteHost);
        System.out.println("RemotePort: " + remotePort);
    }
}