用Java创建Nodelist XML

时间:2022-10-29 17:03:27

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

我在尝试创建可与此json结构匹配的XML文件时遇到困难,

"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,

我的问题是尝试用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);

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.

正如你所见,它已接近但不够接近。任何帮助,将不胜感激。提前致谢。

1 个解决方案

#1


0  

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

字符'['表示集合类型,表示“fixed_ips”在“properties”元素中是集合类型。

i.e class Properties {

即类属性{

   public ArrayList<fixed_ips> list;
       setters & getters
}

class fixed_ips {

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

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

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

fixed_ips f1 = new fixed_ips(); f1.setSid( “东西”);

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

fixed_ips f2 = new fixed_ips(); f2.setSid( “东西”);

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 .

然后你会得到那个'['。但问题是在localList中如果你只添加一个fixed_ibs元素,'''符号变成可选的。如果你在arrayList中有多个元素,那么只有'['会出现。

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

#1


0  

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

字符'['表示集合类型,表示“fixed_ips”在“properties”元素中是集合类型。

i.e class Properties {

即类属性{

   public ArrayList<fixed_ips> list;
       setters & getters
}

class fixed_ips {

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

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

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

fixed_ips f1 = new fixed_ips(); f1.setSid( “东西”);

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

fixed_ips f2 = new fixed_ips(); f2.setSid( “东西”);

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 .

然后你会得到那个'['。但问题是在localList中如果你只添加一个fixed_ibs元素,'''符号变成可选的。如果你在arrayList中有多个元素,那么只有'['会出现。

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