Android 从ftp解析XML到程序

Android 从ftp解析XML到程序,android,xml,parsing,ftp,Android,Xml,Parsing,Ftp,您好, 我正在尝试解析从FTP接收到的XML。我学习了一些教程,但效果不太好。。我的代码是: URLConnection cn; URL url = new URL(SERVICE_URL); cn = url.openConnection(); cn.connect(); InputStream stream = cn.getInputStream();

您好, 我正在尝试解析从FTP接收到的XML。我学习了一些教程,但效果不太好。。我的代码是:

              URLConnection cn;
              URL url = new URL(SERVICE_URL);
              cn = url.openConnection();
              cn.connect();
              InputStream stream = cn.getInputStream();
              DocumentBuilder docBuild = DocumentBuilderFactory.newInstance().newDocumentBuilder();
              Document trDoc = docBuild.parse(stream);

              NodeList nodes = trDoc.getElementsByTagName("Name");
现在,我必须在一个字符串中获取名称,并记录该字符串以进行测试。有人能帮我吗

编辑: 当我这样做时:

         for (int i = 0; i < nodes.getLength(); i++){

              String toe = nodes.item(i).toString();
              Log.d("toe?",""+toe);
             // Log.d("nodes:",""+nodes.item(i))
              /*
            if (nodes.item(i).getTextContent().compareTo(original) == 0){
             System.out.println(nodes.item(i).getTextContent() + " > " + newValue);
             nodes.item(i).setTextContent(newValue);
            }*/
          }
XML代码是:

  <?xml version="1.0" encoding="UTF-8"?>
<Personnel>
  <Employee type="permanent">
        <Name>Seagull</Name>
        <Id>3674</Id>
        <Age>34</Age>
   </Employee>
  <Employee type="contract">
      <Name>Tom</Name>
</Personnel>

  
海鸥
        3674
        34
   
  
汤姆

关于

在我的博客上查看最新教程:
它附带了一个完整的源代码Eclipse项目

希望有帮助

编辑:

尝试更改:

NodeList nodes = trDoc.getElementsByTagName("Name");


您还可以使用sax解析器。。。它可以是这样的:

SAXParserFactory spf = SAXParserFactory.newInstance();
SAXParser sp = spf.newSAXParser();
XMLReader xr = sp.getXMLReader();

URL sourceUrl = new URL(xmlURL);
xr.setContentHandler(myXMLHandler);
xr.parse(new InputSource(sourceUrl.openStream()));
public class MyXMLHandler extends DefaultHandler 
{

    Boolean currentElement = false;
    String currentValue = null;


    @Override
    public void startElement(String uri, String localName, String qName,Attributes attributes)
    throws SAXException 
    {
        currentElement = true;               
    }

    @Override
    public void endElement(String uri, String localName, String qName)
    throws SAXException 
    {
        currentElement = false;
    }


    @Override
    public void characters(char[] ch, int start, int length)
    throws SAXException 
    {
        if (currentElement) 
        {
            currentValue = new String(ch, start, length);
            currentElement = false;
        }


    }

}
现在您需要有一个myXMLHandler类来扩展表单DefaultHandler,如下所示:

SAXParserFactory spf = SAXParserFactory.newInstance();
SAXParser sp = spf.newSAXParser();
XMLReader xr = sp.getXMLReader();

URL sourceUrl = new URL(xmlURL);
xr.setContentHandler(myXMLHandler);
xr.parse(new InputSource(sourceUrl.openStream()));
public class MyXMLHandler extends DefaultHandler 
{

    Boolean currentElement = false;
    String currentValue = null;


    @Override
    public void startElement(String uri, String localName, String qName,Attributes attributes)
    throws SAXException 
    {
        currentElement = true;               
    }

    @Override
    public void endElement(String uri, String localName, String qName)
    throws SAXException 
    {
        currentElement = false;
    }


    @Override
    public void characters(char[] ch, int start, int length)
    throws SAXException 
    {
        if (currentElement) 
        {
            currentValue = new String(ch, start, length);
            currentElement = false;
        }


    }

}

现在,当此类获取任何起始元素时,将自动调用StartElement()。。。characters函数获取任何值……

您可以发布一些示例XML吗?blijkbaar wilt hij XMLfunctions niet doen.)尝试从我的博客下载eclipse项目。我想这会对你有帮助。你也可以随时在那里留下评论。哦,我正试图编辑,所以我想我删除了你的?我是这里的傻瓜。。sry=)你能再做一次吗=$我的代码中似乎没有运行它。我应该搜索“parmanent”吗?“名字”是什么?“人事”?还有,你使用了httpRequest,这是为什么?好吧,我试过你的源代码,现在它可以工作了,只有当我使用你的xml,并将它粘贴到我的ftp位置()时,它才会显示“geen resultaten gevonden”。我猜这是因为你使用了httpPost?如何将此xml集成到http中?还是有其他方法可以做到这一点?