简体   繁体   中英

Create a Nodelist XML in Java

I'm having difficulty trying to create a XML file that could match with this json structure,

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

My problem is trying to create in XML, the symbols [ ] that appears after the Node "fixed_ips" . What I have tried is this,

//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);

And this is what I got,

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

As u see, it's close but not close enough. Any help would be appreciated. Thanks in advance.

The character '[' indicates of a collection type that means , the "fixed_ips" is of collection type in "properties" element.

ie class Properties {

   public ArrayList<fixed_ips> list;
       setters & getters
}

class fixed_ips {

   public Subnet_id sid;
       setters & getters
}

in document creation class it should be like

Properties p=new Properties; ArrayList locallist=p.getList();

fixed_ips f1=new fixed_ips(); f1.setSid("something");

fixed_ips f2=new fixed_ips(); f2.setSid("something");

locallist.add(f2);

p.setList(localList);

Then you will get that '[' . But the thing is that in localList if u add only one element of fixed_ibs that '[' symbol becomes optional. if you have more than one element in the arrayList then only '[' will come .

Or

<?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>

u will get that [.

output will be

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

Thank you

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM