Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用javascript的xml,属性缺少结束标记_Javascript_Xml - Fatal编程技术网

使用javascript的xml,属性缺少结束标记

使用javascript的xml,属性缺少结束标记,javascript,xml,Javascript,Xml,我正在尝试使用JavaScript编写一个XML,如下所示 <Service> <NewInstance ref="External_UCSD_Serverinfo"> <Std>DiscoveredElement</Std> <Virtual/> <Key>Key001</Key> <Attributes> <Attribute name="hp

我正在尝试使用JavaScript编写一个XML,如下所示

<Service>
<NewInstance ref="External_UCSD_Serverinfo">
    <Std>DiscoveredElement</Std>
    <Virtual/>
    <Key>Key001</Key>
    <Attributes>
        <Attribute name="hpom_citype" value="External_UCSD_Serverinfo"/>
    </Attributes>
</NewInstance>
</Service>
但结果是,我的产量低于预期

   <Service>
   <NewInstance ref='External_UCSD_Serverinfo'/>
      <Std>DiscoveredElement</Std>
      <Virtual/>
      <Key>Key001</Key>
      <Attributes>
         <Attribute name='hpom_citype' value='External_UCSD_Serverinfo'/>
   </Attributes>
   </Service>

发现元素
键001

所以我得到了我想要的,除了“NewInstance”的结束标签。能告诉我我错过了什么吗?还有使用JavaScript编写XML内容的简单方法吗?

原因是节点Std、Virtual、Key等被附加到rootElement,而不是NewInstance\u节点

详细内容:

var doc = document.implementation.createDocument(null, null);
var rootElement = doc.createElement("Service");

var NewInstance_node = doc.createElement("NewInstance");
var attr = doc.createAttribute("ref");
attr.value="External_UCSD_Serverinfo";
NewInstance_node.setAttributeNode(attr);
rootElement.appendChild(NewInstance_node);

var Std_node = doc.createElement("Std");
Std_node.appendChild(doc.createTextNode("DiscoveredElement"));
NewInstance_node.appendChild(Std_node);

var Std_Virtual = doc.createElement("Virtual");
NewInstance_node.appendChild(Std_Virtual);

var Key_node = doc.createElement("Key");
Key_node.appendChild(doc.createTextNode("Key001"));
NewInstance_node.appendChild(Key_node);

var CIAttributes_node = doc.createElement("Attributes");
var CIAttribute_node1 = doc.createElement("Attribute");
var attr_name1 = doc.createAttribute("name");
attr_name1.value="hpom_citype";
var attr_val1 = doc.createAttribute("value");
attr_val1.value="External_UCSD_Serverinfo";
CIAttribute_node1.setAttributeNode(attr_name1);
CIAttribute_node1.setAttributeNode(attr_val1);  
rootElement.appendChild(CIAttributes_node); 
CIAttributes_node.appendChild(CIAttribute_node1);

doc.appendChild(rootElement);
var doc = document.implementation.createDocument(null, null);
var rootElement = doc.createElement("Service");

var NewInstance_node = doc.createElement("NewInstance");
var attr = doc.createAttribute("ref");
attr.value="External_UCSD_Serverinfo";
NewInstance_node.setAttributeNode(attr);
rootElement.appendChild(NewInstance_node);

var Std_node = doc.createElement("Std");
Std_node.appendChild(doc.createTextNode("DiscoveredElement"));
NewInstance_node.appendChild(Std_node);

var Std_Virtual = doc.createElement("Virtual");
NewInstance_node.appendChild(Std_Virtual);

var Key_node = doc.createElement("Key");
Key_node.appendChild(doc.createTextNode("Key001"));
NewInstance_node.appendChild(Key_node);

var CIAttributes_node = doc.createElement("Attributes");
var CIAttribute_node1 = doc.createElement("Attribute");
var attr_name1 = doc.createAttribute("name");
attr_name1.value="hpom_citype";
var attr_val1 = doc.createAttribute("value");
attr_val1.value="External_UCSD_Serverinfo";
CIAttribute_node1.setAttributeNode(attr_name1);
CIAttribute_node1.setAttributeNode(attr_val1);  
rootElement.appendChild(CIAttributes_node); 
CIAttributes_node.appendChild(CIAttribute_node1);

doc.appendChild(rootElement);