繁体   English   中英

需要帮助为Twitter Oauth解析XML文件

[英]Need help parsing XML file for Twitter Oauth

我已经编写了一个Twitter桌面应用程序,基本上可以让我发布推文和图片...没什么花哨的。

我已经完成了所有工作,但是保留配置文件(这是我的应用程序生成的以下XML)的最后一部分。

<?xml version="1.0" encoding="UTF-8" standalone="no"?><Twitterer><config id="1"><accessToken>ENDLESS-STRING-OF-CHARACTERS</accessToken><accessTokenSecret>ANOTHER-ENDLESS-STRING-OF-CHARACTERS</accessTokenSecret></config></Twitterer>

我需要做的只是设置accessToken和accessTokenSecret变量。 文件名是config.xml。

我一直在网上查看许多示例,但似乎无法从文件中仅获取两个值来解决问题,这不需要循环。

这是我对难题的最后一部分的理解:

try {
        File fXmlFile = new File(this.getFileName());
        DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
        DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
        Document doc = dBuilder.parse(fXmlFile);
        doc.getDocumentElement().normalize();

        NodeList nList = doc.getElementsByTagName("config");
        int numberOfConfigs = nList.getLength();

        // GET THE TWO VARIABLES HERE

    } catch (Exception e) {

    }

如果有人可以帮助我将这两个标签读入它们相应的变量中,我将不胜感激。 之后,我可以处理其余的授权。

我需要做的只是设置accessToken和accessTokenSecret变量

使用getElementsByTagName()方法的简单代码

Element root = doc.getDocumentElement();   
root.getElementsByTagName("accessToken").item(0).getTextContent()
root.getElementsByTagName("accessTokenSecret").item(0).getTextContent()

输出:

ENDLESS-STRING-OF-CHARACTERS
ANOTHER-ENDLESS-STRING-OF-CHARACTERS

或尝试作为config标签的子节点

Element root = doc.getDocumentElement();
NodeList configNodeList = root.getElementsByTagName("config");
NodeList nodeList = ((Node) configNodeList.item(0)).getChildNodes();
System.out.println(nodeList.item(0).getTextContent());
System.out.println(nodeList.item(1).getTextContent());

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM