簡體   English   中英

用Java創建節點列表XML

[英]Create a Nodelist XML in Java

我在嘗試創建可以與此json結構匹配的XML文件時遇到困難,

"properties": {
    "network_id": {
      "get_resource": "private_net"
    }, 
    "fixed_ips": [
      {
        "subnet_id": {
          "get_resource": "private_subnet"
        }
      }
    ]
  }

我的問題是嘗試在XML中創建出現在節點“ fixed_ips”之后的符號[]。 我嘗試過的是

//This part of the code is in a for loop
Element propertiesn = doc.createElement("properties");
serverports.appendChild(propertiesn);
Element networkidd = doc.createElement("network_id");
propertiesn.appendChild(networkidd);
Element getress = doc.createElement("get_resource");
getress.appendChild(doc.createTextNode("private_net"));
networkidd.appendChild(getress);

Element fixesips = doc.createElement("fixed_ips");
propertiesn.appendChild(fixesips);
Element qq = doc.createElement("qq");
fixesips.appendChild(qq);
Element subnetid = doc.createElement("subnet_id");
qq.appendChild(subnetid);
Element getresss = doc.createElement("get_resource");
getresss.appendChild(doc.createTextNode("private_subnet"+i6));
subnetid.appendChild(getresss);
Element qq2 = doc.createElement("qq");
fixesips.appendChild(qq2);

這就是我得到的

 "properties": {
    "network_id": {
      "get_resource": "private_net"
    },
    "fixed_ips": [
      {
        "subnet_id": {
          "get_resource": "private_subnet1"
        }
      },
      []
    ]
  }

如您所見,它接近但不夠接近。 任何幫助,將不勝感激。 提前致謝。

字符“ [”表示收集類型,這意味着“ fixed_ips”是“屬性”元素中的收集類型。

即類屬性{

   public ArrayList<fixed_ips> list;
       setters & getters
}

class fixed_ips {

   public Subnet_id sid;
       setters & getters
}

在文檔創建類中,它應該像

特性p =新特性; ArrayList locallist = p.getList();

fixed_ips f1 =新的fixed_ips(); f1.setSid(“ something”);

fixed_ips f2 = new fixed_ips(); f2.setSid(“ something”);

locallist.add(f2);

p.setList(localList);

然后您將得到'['。 但是問題是,如果u僅添加fixed_ibs的一個元素,則'['符號變為可選。 如果arrayList中有多個元素,則只有'['會出現。

要么

<?xml version="1.0" encoding="UTF-8" ?>
    <properties>
        <network_id>
            <get_resource>private_net</get_resource>
        </network_id>
        <fixed_ips>
            <subnet_id>
                <get_resource>private_subnet</get_resource>
            </subnet_id>
        </fixed_ips>
                <fixed_ips>
            <subnet_id>
                <get_resource>private_subnet</get_resource>
            </subnet_id>
        </fixed_ips>
    </properties>

你會得到[。

輸出將是

{
  "properties": {
    "network_id": { "get_resource": "private_net" },
    "fixed_ips": [
      {
        "subnet_id": { "get_resource": "private_subnet" }
      },
      {
        "subnet_id": { "get_resource": "private_subnet" }
      }
    ]
  }
}

謝謝

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM