0

I am trying to prepare a XML using JavaScript which should look like below.

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

i have prepared below code.

var doc = builder.newDocument();
var rootElement = doc.createElement("Service");

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


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

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


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


var CIAttributes_node = doc.createElement("Attributes");
var CIAttribute_node1 = doc.createElement("Attribute");
var attr_name1 = doc.createAttribute("name");
attr_name1.setValue("hpom_citype");
var attr_val1 = doc.createAttribute("value");
attr_val1.setValue("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 tf = javax.xml.transform.TransformerFactory.newInstance();
var t = tf.newTransformer();
t.setOutputProperty("omit-xml-declaration", "yes");
var sw = new StringWriter();
t.transform(new javax.xml.transform.dom.DOMSource(doc), new javax.xml.transform.stream.StreamResult(sw));

but as a result i get below Output.

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

So i am getting what i am looking for except the end tag of "NewInstance". can soomeone tell me what i am missing? also is there simple way of writing XML Content using JavaScript?

1 Answer 1

1

the reason is that the nodes Std, Virtual, Key etc. are appended to rootElement instead of NewInstance_node

in detail:

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);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.