首页 \ 问答 \ Java - 使用DOM解析xml(Java - Parsing xml using DOM)

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
更新时间:2022-07-12 11:07

最满意答案

我认为没有必要恢复遥控器。 您可以简单地将本地仓库推送到新的远程仓库。 这是使分布式源代码控制如此受欢迎的原因之一,不是吗?


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 ...
  • 我认为没有必要恢复遥控器。 您可以简单地将本地仓库推送到新的远程仓库。 这是使分布式源代码控制如此受欢迎的原因之一,不是吗? 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 ...
  • 遥控器可能只是Windows服务器上映射网络驱动器内的文件夹吗? 我想知道因为我看到了一系列与遥控器连接的选项,包括本地文件。 是的,它可以像本地文件夹一样简单。 当然,如果您不需要任何身份验证或授权。 我想我的问题归结为,是否有git服务器必须处理的特殊git协议,或者它真的只是一种服务构成存储库的文件的方法? HTTP上有两个协议,SSH上有另一个协议。 我相信HTTP协议只对所有人都是只读的,没有授权,但我不记得了。 SSH协议是在SSH访问的任何计算机上设置git服务器的简便方法。 在下面,每次调用 ...

相关文章

更多

最新问答

更多
  • h2元素推动其他h2和div。(h2 element pushing other h2 and div down. two divs, two headers, and they're wrapped within a parent div)
  • 创建一个功能(Create a function)
  • 我投了份简历,是电脑编程方面的学徒,面试时说要培训三个月,前面
  • PDO语句不显示获取的结果(PDOstatement not displaying fetched results)
  • Qt冻结循环的原因?(Qt freezing cause of the loop?)
  • TableView重复youtube-api结果(TableView Repeating youtube-api result)
  • 如何使用自由职业者帐户登录我的php网站?(How can I login into my php website using freelancer account? [closed])
  • SQL Server 2014版本支持的最大数据库数(Maximum number of databases supported by SQL Server 2014 editions)
  • 我如何获得DynamicJasper 3.1.2(或更高版本)的Maven仓库?(How do I get the maven repository for DynamicJasper 3.1.2 (or higher)?)
  • 以编程方式创建UITableView(Creating a UITableView Programmatically)
  • 如何打破按钮上的生命周期循环(How to break do-while loop on button)
  • C#使用EF访问MVC上的部分类的自定义属性(C# access custom attributes of a partial class on MVC with EF)
  • 如何获得facebook app的publish_stream权限?(How to get publish_stream permissions for facebook app?)
  • 如何防止调用冗余函数的postgres视图(how to prevent postgres views calling redundant functions)
  • Sql Server在欧洲获取当前日期时间(Sql Server get current date time in Europe)
  • 设置kotlin扩展名(Setting a kotlin extension)
  • 如何并排放置两个元件?(How to position two elements side by side?)
  • 如何在vim中启用python3?(How to enable python3 in vim?)
  • 在MySQL和/或多列中使用多个表用于Rails应用程序(Using multiple tables in MySQL and/or multiple columns for a Rails application)
  • 如何隐藏谷歌地图上的登录按钮?(How to hide the Sign in button from Google maps?)
  • Mysql左连接旋转90°表(Mysql Left join rotate 90° table)
  • dedecms如何安装?
  • 在哪儿学计算机最好?
  • 学php哪个的书 最好,本人菜鸟
  • 触摸时不要突出显示表格视图行(Do not highlight table view row when touched)
  • 如何覆盖错误堆栈getter(How to override Error stack getter)
  • 带有ImageMagick和许多图像的GIF动画(GIF animation with ImageMagick and many images)
  • USSD INTERFACE - > java web应用程序通信(USSD INTERFACE -> java web app communication)
  • 电脑高中毕业学习去哪里培训
  • 正则表达式验证SMTP响应(Regex to validate SMTP Responses)