Java - 使用DOM解析xml(Java - Parsing xml using DOM)
我试图解析以下的xml。 我可以轻松访问WeekNumber但无法访问EmployeeRatesLevelA和EmployeeRatesLevelB的子项。 目标是将这些保存到具有字段WeekNumber和ArrayLists,EmployeeRatesLevelA和EmployeeRatesLevelB的类DataSet。 谢谢。
<DataSet ActiveFrom="2011/04/06"> <WeekNumber>8</WeekNumber> <EmployeeRatesLevelA> <Rate>0</Rate> <Rate>0.12</Rate> </EmployeeRatesLevelA> <EmployeeRatesLevelB> <Rate>0.15</Rate> <Rate>0.20</Rate> </EmployeeRatesLevelB> </DataSet> Document doc = loadXml("data.xml"); NodeList nodeList = doc.getElementsByTagName("DataSet"); for (int i = 0; i < nodeList.getLength(); i++) { Node node = nodeList.item(i); if (node.getNodeType() == Node.ELEMENT_NODE) { Element element = (Element) node; NodeList weekNumberList = element.getElementsByTagName("WeekNumber"); Element weekElement = (Element) weekNumberList.item(0); NodeList textElementList = weekElement.getChildNodes(); System.out.println("Weeknumber:"+ ((Node)textElementList.item(0)).getNodeValue().trim()); } public static Document loadXml(String file) { try { return (DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(new File(file))); } catch (SAXException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } catch (ParserConfigurationException e) { e.printStackTrace(); } return null; }
这给了我Weeknumber但我无法访问EmployeeRatesLevelA和EmployeeRatesLevelB。
想学习其他很酷的东西但是因为我是Java新手并且xml文档非常小,DOM应该足够了。
I am trying to parse the following xml. I can access the WeekNumber easily but cannot access the children for EmployeeRatesLevelA and EmployeeRatesLevelB. The goal is to save these to a class, DataSet with fields WeekNumber and ArrayLists, EmployeeRatesLevelA and EmployeeRatesLevelB. Thanks.
<DataSet ActiveFrom="2011/04/06"> <WeekNumber>8</WeekNumber> <EmployeeRatesLevelA> <Rate>0</Rate> <Rate>0.12</Rate> </EmployeeRatesLevelA> <EmployeeRatesLevelB> <Rate>0.15</Rate> <Rate>0.20</Rate> </EmployeeRatesLevelB> </DataSet> Document doc = loadXml("data.xml"); NodeList nodeList = doc.getElementsByTagName("DataSet"); for (int i = 0; i < nodeList.getLength(); i++) { Node node = nodeList.item(i); if (node.getNodeType() == Node.ELEMENT_NODE) { Element element = (Element) node; NodeList weekNumberList = element.getElementsByTagName("WeekNumber"); Element weekElement = (Element) weekNumberList.item(0); NodeList textElementList = weekElement.getChildNodes(); System.out.println("Weeknumber:"+ ((Node)textElementList.item(0)).getNodeValue().trim()); } public static Document loadXml(String file) { try { return (DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(new File(file))); } catch (SAXException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } catch (ParserConfigurationException e) { e.printStackTrace(); } return null; }
This gives me the Weeknumber but I am unable to access EmployeeRatesLevelA and EmployeeRatesLevelB.
Would like to learn other cool stuff but as I am new to Java and the xml document is really small, DOM should suffice.
原文:https://stackoverflow.com/questions/8345529
最满意答案
我认为没有必要恢复遥控器。 您可以简单地将本地仓库推送到新的远程仓库。 这是使分布式源代码控制如此受欢迎的原因之一,不是吗?
I don't think there was a need to restore the remote. You could simply push your local repo to a new remote one. That's one of the reasons that made distributed source control so popular, isn't it?
相关问答
更多-
在服务器上创建一个新的裸仓库: git init --bare newrepo.git 将其作为远程添加到您的本地回购: git remote add newrepo git://user@server.com/newrepo.git git push newrepo master推送一个特定的分支,或者 git push --all newrepo推送所有分支 Create a fresh bare repository on the server: git init --bare newrepo.git ...
-
是的,这称为自动构建或持续集成。 有许多工具可以让你这样做,但很难在不知道你的开发平台的情况下推荐一个( TeamCity ,我最喜欢的,与git和许多开发平台一起工作)。 根据您的网站的用途,在每次提交时将代码推送到生产中通常存在风险。 通常这个过程比那个更受控制。 Yes, this is called an automated build, or Continuous Integration. There are many tools that allow you to do this, but it ...
-
行git remote add foo myUser@git.mydomain.com:foo.git假定目录foo.git位于myUser用户的主目录中。 你使用绝对路径好得多,例如git remote add foo myUser@git.mydomain.com:/home/gitrepos/foo.git 如果您在共享服务器上,则可以通过执行以下命令来检查您的主路径: echo $HOME 就我而言,这产生了: 家用/ U / 01234 / 如果我已将自己的存储库存储在主路径的www / git ...
-
GIT:使用远程git存储库还原服务器 - 如何正确行事?(GIT: restoring server with remote git repository - how to act right?)[2024-01-13]
我认为没有必要恢复遥控器。 您可以简单地将本地仓库推送到新的远程仓库。 这是使分布式源代码控制如此受欢迎的原因之一,不是吗? I don't think there was a need to restore the remote. You could simply push your local repo to a new remote one. That's one of the reasons that made distributed source control so popular, isn't ... -
你必须使用git push。 git add "directory/fileName" git commit -m "your comment here" git push origin master 有关更多详细信息: 如何在GitHub上传文件 You have to use git push for that. git add "directory/fileName" git commit -m "your comment here" git push origin master ...
-
您不必过多担心裸存储库中的blob。 更好的理解是每个存储库都由提交对象组成,这些对象在提交到该存储库时包含确切的文件结构。 在您的示例中,用户A在本地生成commitA,用户B在本地生成commitB,它们都基于裸存储库上的master,它将指向提交对象(例如)commitBase。 这意味着它们都具有commitBase的父级。 当A推送时,他将其commitA推送到裸存储库,该存储库将主分支指针移动到commitA。 有关file1的所有信息都在commit对象commitA中(以及有关文件结构其余部 ...
-
这就是我解决问题的方法: 我配置了本地存储库 我在远程服务器上创建了一个裸存储库 git init --bare 我已采取必要的步骤来配置提交到远程仓库 我在远程存储库的hooks文件夹下添加了一个post-receive钩子 / path / to / repo / hooks / post-receive : #!/bin/sh GIT_WORK_TREE=/var/www/www.example.org git checkout -f 然后我知道它: chmod +x /path/to/repo/h ...
-
为了简化到略微过于简单化的程度,在Git中, 远程只是一个简单的名称,如origin或upstream或github或beeblebrox或其他什么,用于(如在一个单一的)URL。 (实际上,您可以将多个URL附加到任何一个远程名称,但这并不适用于此类用法。) 您一直在使用一个URL,您一直在调用origin 。 现在您希望使用其他URL。 您无法添加第二个origin 。 您可以添加第二个遥控器 ,但不能将其称为origin ,因为您已有origin 。 如果你想打电话给这个新的远程howdy-doody ...
-
你会做这些步骤: 在服务器上创建单个存储库。 git clone --bare nameofrepo 在实际存储库中,将远程数据库添加到要从中发送工作的存储库: git remote add origin
现在推动你的分支工作: git push origin master 对要推送到中央仓库的任何其他分支重复此操作。 第一个命令中的URL也可以是常规文件路径。 大多数解决方案都是通过SSH连接。 You would do these steps: Make ... -
什么是git服务器?(What exactly is a git server?)[2022-06-14]
遥控器可能只是Windows服务器上映射网络驱动器内的文件夹吗? 我想知道因为我看到了一系列与遥控器连接的选项,包括本地文件。 是的,它可以像本地文件夹一样简单。 当然,如果您不需要任何身份验证或授权。 我想我的问题归结为,是否有git服务器必须处理的特殊git协议,或者它真的只是一种服务构成存储库的文件的方法? HTTP上有两个协议,SSH上有另一个协议。 我相信HTTP协议只对所有人都是只读的,没有授权,但我不记得了。 SSH协议是在SSH访问的任何计算机上设置git服务器的简便方法。 在下面,每次调用 ...