如何创建一个循环来计算“孩子”列表中的特定“孩子”?

时间:2016-11-15 00:30:22

标签: xml xml-parsing jtree jdom-2

考虑以下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="$20.00" />
                <user planName="lite" cost="$10.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

关于这一点,我需要每个孩子"props""section""queues""user"都有自己的标签,所以我必须计算每一个这些子元素在其父元素计划中。

我创建了一个循环,用于计算其父"Plan"中的子"Plans"的编号。怎么样?

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

请注意,"List<Element>"表示“列表”,其中包含“儿童”合奏。

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

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

PlansProps

如何使用“for(int)”循环来计算某个“元素”而不是“元素列表”

1 个答案:

答案 0 :(得分:0)

我找到了一种方法来实现我的目标,为第二个循环中声明的Element实现另一个方法[set mapred.job.queue.name=root.apps10; set spark.master=yarn-client; set hive.server2.enable.doAs=false; set hive.execution.engine=spark; set spark.eventLog.enabled=true; set spark.shuffle.blockTransferService=nio; set spark.eventLog.dir=hdfs://HDFSNode:8020/user/spark/applicationHistory; set spark.shuffle.service.enabled=true; set spark.dynamicAllocation.enabled=true; set hive.spark.job.monitor.timeout=180s; DROP TABLE IF EXISTS testhiveonspark.temptable2; CREATE TABLE testhiveonspark.temptable2 STORED AS TEXTFILE AS SELECT num1, num2 FROM testhiveonspark.temptable1; ],我们可以使用方法[.getQualifiedName()]作为循环计数器:

.getSize()

And now this is how does it look like: