function WriteTest() { try { var XML=new XMLWriter(); XML.BeginNode("Example"); XML.Attrib("SomeAttribute", "And Some Value"); XML.Attrib("AnotherAttrib", "..."); XML.WriteString("This is an example of the JS XML WriteString method."); XML.Node("Name", "Value"); XML.BeginNode("SubNode"); XML.BeginNode("SubNode2"); XML.EndNode(); XML.BeginNode("SubNode3"); XML.WriteString("Blah blah."); XML.EndNode(); XML.Close(); // Takes care of unended tags. // The replace in the following line are only for making the XML look prettier in the textarea. document.getElementById("ExampleOutput").value=XML.ToString().replace(/</g,"/n<"); } catch(Err) { alert("Error: " + Err.description); } return false; }
生成的xml为:
<Example SomeAttribute="And Some Value" AnotherAttrib="...">This is an example of the JS XML WriteString method. <Name>Value </Name> <SubNode> <SubNode2/> <SubNode3>Blah blah. </SubNode3> </SubNode> </Example>