Android FTP inputstream到XML
嗨,我正在尝试从FTP服务器读取XML文件并对其进行解析 但是我不能使用我的字符串作为输入源。我可以看到我可以在LogCat中读取文件,并且可以在浏览器中打开XMl文件。但是我不确定如何使用字符串作为输入源 代码: 我不确定StringReader是否正确 编辑: 多亏了拉杰什 这是我的解决方案: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
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
你有什么错误吗?如果是,请张贴。从
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);