[英]Need help parsing XML file for Twitter Oauth
I've written a twitter desktop app that basically just lets me post tweets and pics... nothing fancy. 我已经编写了一个Twitter桌面应用程序,基本上可以让我发布推文和图片...没什么花哨的。
I've got everything working but this last part of persisting a config file (which is the following XML generated by my application. 我已经完成了所有工作,但是保留配置文件(这是我的应用程序生成的以下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>
What I need to do is just set the accessToken & accessTokenSecret variables. 我需要做的只是设置accessToken和accessTokenSecret变量。 The filename is config.xml.
文件名是config.xml。
I've been looking at a lot of examples on the net, but can't seem to wrap my head around only getting two values from the file, which shouldn't need a loop. 我一直在网上查看许多示例,但似乎无法从文件中仅获取两个值来解决问题,这不需要循环。
This is as far as I've gotten on this last piece of my puzzle: 这是我对难题的最后一部分的理解:
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) {
}
If anyone can help me just read those two tags into their corresponding variables I would be quite appreciative. 如果有人可以帮助我将这两个标签读入它们相应的变量中,我将不胜感激。 I can handle the rest of the Authorization after that.
之后,我可以处理其余的授权。
What I need to do is just set the accessToken & accessTokenSecret variables
我需要做的只是设置accessToken和accessTokenSecret变量
A simple code using getElementsByTagName()
method 使用
getElementsByTagName()
方法的简单代码
Element root = doc.getDocumentElement();
root.getElementsByTagName("accessToken").item(0).getTextContent()
root.getElementsByTagName("accessTokenSecret").item(0).getTextContent()
output: 输出:
ENDLESS-STRING-OF-CHARACTERS
ANOTHER-ENDLESS-STRING-OF-CHARACTERS
OR try as child node of config
tag 或尝试作为
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.