Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/201.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android FTP inputstream到XML_Android_Xml_Ftp - Fatal编程技术网

Android FTP inputstream到XML

Android FTP inputstream到XML,android,xml,ftp,Android,Xml,Ftp,嗨,我正在尝试从FTP服务器读取XML文件并对其进行解析 但是我不能使用我的字符串作为输入源。我可以看到我可以在LogCat中读取文件,并且可以在浏览器中打开XMl文件。但是我不确定如何使用字符串作为输入源 代码: 我不确定StringReader是否正确 编辑: 多亏了拉杰什 这是我的解决方案: public void readXML(){ try { Log.e("FTP", "Starting ftp session"); FTPClient f

嗨,我正在尝试从FTP服务器读取XML文件并对其进行解析

但是我不能使用我的字符串作为输入源。我可以看到我可以在LogCat中读取文件,并且可以在浏览器中打开XMl文件。但是我不确定如何使用字符串作为输入源

代码:

我不确定StringReader是否正确

编辑:

多亏了拉杰什

这是我的解决方案:

public void readXML(){

    try {
        Log.e("FTP", "Starting ftp session");

        FTPClient ftp = new FTPClient();

        Log.e("FTP", "Connecting to FTP");
        ftp.connect("ftp.domain.com");

        Log.e("FTP", "Providing credentials to FTP");
        ftp.login("user", "pswd");
        if(ftp.login("user", "pswd")){
            Log.e("FTP", "Was able to connect to FTP");
        }

        ftp.enterLocalPassiveMode();
        ftp.changeWorkingDirectory("/folder/XML");

        Log.e("FTP", "Getting stream");
        InputStream inStream = ftp.retrieveFileStream("file.xml");          

        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        DocumentBuilder db = dbf.newDocumentBuilder();

        Log.e("FTP", "Parsing inputsource");
        Document doc = db.parse(new InputSource(inStream));
        doc.getDocumentElement().normalize();

        Log.e("FTP", "Creating nodelist");
        NodeList nodeList = doc.getElementsByTagName("TAG");

一些建议/意见:

  • 您可以直接从
    InputStream
    创建
    InputSource
    ,而无需中间
    字符串
  • 使用
    StringBuffer
  • 使用以下代码将导致跳过备用行(可能是无效的XML-可能是您的问题所在)


  • 你有什么错误吗?如果是,请张贴。从
    StringReader
    创建
    InputSource
    完全有效。谢谢!Document doc=db.parse(新输入源(流内));
    public void readXML(){
    
        try {
            Log.e("FTP", "Starting ftp session");
    
            FTPClient ftp = new FTPClient();
    
            Log.e("FTP", "Connecting to FTP");
            ftp.connect("ftp.domain.com");
    
            Log.e("FTP", "Providing credentials to FTP");
            ftp.login("user", "pswd");
            if(ftp.login("user", "pswd")){
                Log.e("FTP", "Was able to connect to FTP");
            }
    
            ftp.enterLocalPassiveMode();
            ftp.changeWorkingDirectory("/folder/XML");
    
            Log.e("FTP", "Getting stream");
            InputStream inStream = ftp.retrieveFileStream("file.xml");          
    
            DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
            DocumentBuilder db = dbf.newDocumentBuilder();
    
            Log.e("FTP", "Parsing inputsource");
            Document doc = db.parse(new InputSource(inStream));
            doc.getDocumentElement().normalize();
    
            Log.e("FTP", "Creating nodelist");
            NodeList nodeList = doc.getElementsByTagName("TAG");
    
    do{
        Input = Input + reader.readLine() + "\n";
        Log.e("FTP", reader.readLine());
    
    }while(reader.readLine()!=null);