Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

How can I create an XML object from scratch?

Explorer ,
Dec 16, 2020 Dec 16, 2020

I can read a file containing XML into an XML object, find descendants that have particular values, and add properties to the XML object.  But I don't know how to create an XML object from scratch.

 

var xmlFile = new File("c:\\misc\\note.xml");
if (xmlFile.open("r"))
{
alert ("XML file opened.");
var xmlString = xmlFile.read();
var xmlObject = XML(xmlString);
alert ("Original XML:\n" + xmlObject);
/*
var newChild = new XML();
newChild.name = "Title";
newChild.text = "Waterfall";
xmlObject.appendChild(newChild);
*/
alert ("Heading is " + xmlObject.heading);
xmlObject.title = "Waterfall";
// var titleObject = new XML("<Title>Waterfall</Title>");
// titleObject.appendChild("Waterfall");
// alert ("Title object:\n" + titleObject);
// xmlObject.appendChild(titleObject);
alert ("XML is now \n" + xmlObject);
var titleObject = xmlObject.descendants("title");
alert ("Title object: " + titleObject);
titleObject.SubTitle = "This Is A Subtitle";
alert ("XML is now \n" + xmlObject);

var anotherXmlObject = new XML("Root");
anotherXmlObject.SomeProperty = "SomeValue";
alert("Another XML object:\n" + anotherXmlObject.toXMLString());
}

 

In the above code, I read a file, display the XML content of the file, add an element named "title" with the text "Waterfall", and add an element named "SubTitle" with the text "This is a subtitle".  An alert statement shows me that that all works.  Now I want to create a new XML object that would contain:

<Root>
<SomeProperty>SomeValue</SomeProperty>
</Root>

But the last alert only shows me "Root".  

 

What am I doing wrong?

TOPICS
Actions and scripting
234
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Adobe
Community Expert ,
Dec 16, 2020 Dec 16, 2020
LATEST

Try the following

var anotherXmlObject = new XML("<Root>");
anotherXmlObject.SomeProperty = "SomeValue";
alert("Another XML object:\n" + anotherXmlObject);

-Manan

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines