我在下面有XML参数;如您所见,这是两个“网关”节点。第一个有接口内部,第二个有接口Uplink。
我想添加新的 IpRange , StartAddress 和 EndAddress 子节点。但首先,我需要找到具有上行链路接口的Gateway节点,并在此处附加IpRange参数。像x.x.x.x。
我是XML新手,我使用的是javascript和HTML DOM。
<Gateways>
<Gateway>
<Interface>internal</Interface>
<Subnet>
<Gateway>1.1.1.2</Gateway>
<Netmask>255.255.255.0</Netmask>
<IpAddress>1.1.1.1</IpAddress>
</Subnet>
</Gateway>
<Gateway>
<Interface>uplink</Interface>
<Subnet>
<Gateway>2.2.2.2</Gateway>
<Netmask>255.255.255.0</Netmask>
<IpAddress>2.2.2.1</IpAddress>
<IpRanges>
<IpRange>
<StartAddress>x.x.x.x</StartAddress>
<EndAddress>x.x.x.x</EndAddress>
</IpRange>
<IpRange>
<StartAddress>x.x.x.x</StartAddress>
<EndAddress>x.x.x.x</EndAddress>
</IpRange>
</IpRanges>
</Subnet>
</Gateway>
</Gateways>
答案 0 :(得分:0)
try with this JS code mad by XML DOM function:
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function () {
if (this.readyState == 4 && this.status == 200) {
readXML(this);
}
};
xhttp.open("GET", "yourFile.xml", true);
xhttp.send();
function readXML(xml) {
var newElement, xmlDoc;
//New xmlDoc
xmlDoc = xml.responseXML;
//Looking for the right interface node
var InterfaceNode = xmlDoc.getElementsByTagName("Interface").nodeValue;
for (let i = 0; i < xmlDoc.getElementsByTagName("Interface").length; i++) {
//The one with value = 'uplink'
if (xmlDoc.getElementsByTagName("Interface")[i].nodeValue == 'uplink') {
var IpRangesNode = xmlDoc.getElementsByTagName("IpRanges")[i];
//Creating the new node IpRange
newElement = xmlDoc.createElement("IpRange");
//Adding 2 new nodes StartAddress and EndAddress both with x.x.x.x as value
var newStartAddress = newElement.createElement("StartAddress");
newStartAddress.nodeValue = "x.x.x.x";
var newEndAddress = newElement.createElement("EndAddress");
newEndAddress.nodeValue = "x.x.x.x";
//Appending to the initial Node
IpRangesNode.appendChild(newElement);
}
}
}
Hope this could help, Laura.