如何创建一个循环来计算 "Children" 列表中的特定 "Child"?

How can create a loop to count an specific "Child" in a list of "Children"?

考虑以下 XML 文件:

<project>
    <props>
        <prop name="currency" value="USD" />
        <prop name="user-type" value="admin" />
    </props>

    <plans>
        <plan name="-" showFirst="false" showSecond="false">
            <props>
                <prop name="moneyback" value="true" />
                <prop name="capacity" value="5" />
            </props>
            <section id="setup"/>
            <section id="hardware" defaultChoice="machines">
                <parts level="1">
                    <part choice="machine_001" name="DAP010" />
                    <part choice="machine_002" name="DAP011" />
                </parts>
            </section>
            <section id="software" />     
            <queues>
                <queue type="q2" minPlan="CC_Q" />
            </queues>
        </plan>


        <plan name="A" showFirst="true" showSecond="true">
            <users>
                <user planName="classic" cost=".00" />
                <user planName="lite" cost=".00" />
            </users>
        </plan>

        <plan name="B" showFirst="true" showSecond="false">
            <props>
                <prop name="moneyback" value="false" />
                <prop name="capacity" value="2" />
            </props>
            <section id="setup"/>
            <section id="software" />     
            <queues>
                <queue type="q2" minPlan="CC_Q" />
            </queues>
        </plan>

    </plans>
</project>

使用 "JDOM2"、

public void actionPerformed(ActionEvent e) {
  JFileChooser ServiceOfferFileChooser = new JFileChooser();
  ServiceOfferFileChooser.setCurrentDirectory(new File(System.getProperty("user.home")));
  ServiceOfferFileChooser.setAcceptAllFileFilterUsed(false);
  FileNameExtensionFilter filter = new FileNameExtensionFilter("*.xml", "xml");
  ServiceOfferFileChooser.addChoosableFileFilter(filter);

  int resultChangeServiceOfferFileChooser = ServiceOfferFileChooser.showOpenDialog(null);

  if (resultChangeServiceOfferFileChooser == JFileChooser.APPROVE_OPTION) {
    File selectedServiceOfferFile = ServiceOfferFileChooser.getSelectedFile();

    SAXBuilder soRead = new SAXBuilder();

    Document ReadSO;
    try {
      ReadSO = soRead.build(new File(selectedServiceOfferFile.getPath()));

      JTree loadedTree = new JTree();
      loadedTree.setModel(new DefaultTreeModel(
        new DefaultMutableTreeNode("Service Offer") {

          private static final long serialVersionUID = 1L;

          {
            add(new DefaultMutableTreeNode("Service Offer Properties"));

            List<Element> soPlans = ReadSO.getRootElement().getChild("plans").getChildren();
            for(int i=0;i<soPlans.size();i++){
              Element soPlan = soPlans.get(i);
              DefaultMutableTreeNode node_soPlan = new DefaultMutableTreeNode("Plan: "+soPlan.getAttributeValue("name"));
                add(node_soPlan);

                List<Element> plansProps = soPlans.get(i).getChildren();
                for (int ii=0;ii<=plansProps.size()-1;ii++){
                  Element plansProp = plansProps.get(ii);
                  node_soPlan.add(new DefaultMutableTreeNode(plansProp));
                }
            }

          }
        }
      ));
      newTree.setVisible(false);
      westNorthPanel.add(loadedTree);

This is how my JTree looks like


关于我需要 Children "props""section""queues""user" 中的每一个都有自己定义的标签,所以我必须在他们的 parent 元素计划中计算每个 Child 元素。

我创建了一个循环来计算 parent "Plans" 中 Child "Plan" 的数量。怎么样?

List<Element> soPlans = ReadSO.getRootElement().getChild("plans").getChildren();
            for(int i=0;i<soPlans.size();i++){
              Element soPlan = soPlans.get(i);

请注意,"List<Element>" 意味着 "list" 满足 "children" 的合奏。

现在的问题和我的问题是,在第二个循环中,我希望代码访问所有 "Plans" 并且当它找到 "props" 时创建具有所需文本的 JTree 节点。

问题是如果我在方法“.getChildren()”之后使用方法[.getChild("props")],编译器会正确地提示我需要将“List<Element>”更改为“Elemen";通过这样做,方法“.size()”没有为“PlansProps”定义“

List<Element> plansProps = soPlans.get(i).getChildren();
                    for (int ii=0;ii<=plansProps.size()-1;ii++){
                      Element plansProp = plansProps.get(ii);
                      node_soPlan.add(new DefaultMutableTreeNode(plansProp));
                    }

如何使用 "for (int)" 循环来计算某个 "Element" 而不是 "List of Elements"

我找到了一种方法来实现我的目标,方法是为第二个循环内声明的元素实现另一种方法 [.getQualifiedName()],我们可以在其中对循环计数器使用方法 [.getSize()]:

SAXBuilder soRead = new SAXBuilder();

  Document ReadSO;
  try {
    ReadSO = soRead.build(new File(selectedServiceOfferFile.getPath()));

    JTree loadedTree = new JTree();
    loadedTree.setModel(new DefaultTreeModel(
      new DefaultMutableTreeNode("Service Offer") {

        private static final long serialVersionUID = 1L;

        {
          add(new DefaultMutableTreeNode("Service Offer Properties"));

          List<Element> soPlans = ReadSO.getRootElement().getChild("plans").getChildren();
          for(int i=0;i<soPlans.size();i++){
            Element soPlan = soPlans.get(i);
            DefaultMutableTreeNode node_soPlan = new DefaultMutableTreeNode("Plan: "+soPlan.getAttributeValue("name"));
              add(node_soPlan);

          **List<Element> plansProps = soPlans.get(i).getChildren();**
          for (int ii=0;ii<=plansProps.size()-1;ii++){
            Element plansProp = plansProps.get(ii);
            **if (plansProp.getQualifiedName()** == "props"){
              node_soPlan.add(new DefaultMutableTreeNode("Plan Properties"));
            }
            else if (plansProp.getQualifiedName() == "section"){
              //System.out.println(plansProp.getAttributeValue("id"));
              node_soPlan.add(new DefaultMutableTreeNode("Section: "+plansProp.getAttributeValue("id")));
            }
            else if (plansProp.getQualifiedName() == "queues"){
              node_soPlan.add(new DefaultMutableTreeNode("Queue Licences: "));
            }
            else if (plansProp.getQualifiedName() == "users"){
              node_soPlan.add(new DefaultMutableTreeNode("Users: "));
            }
            else {
              node_soPlan.add(new DefaultMutableTreeNode(plansProp));
            }

            System.out.println(plansProp.getAttributeValue("id"));
          }

      }


    }
  }

And now this is how does it look like: