0
How can I create an XML object from scratch?
Explorer
,
/t5/photoshop-ecosystem-discussions/how-can-i-create-an-xml-object-from-scratch/td-p/11686367
Dec 16, 2020
Dec 16, 2020
Copy link to clipboard
Copied
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
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting.
Learn more
Explore related tutorials & articles
Community Expert
,
LATEST
/t5/photoshop-ecosystem-discussions/how-can-i-create-an-xml-object-from-scratch/m-p/11686541#M495474
Dec 16, 2020
Dec 16, 2020
Copy link to clipboard
Copied
Try the following
var anotherXmlObject = new XML("<Root>");
anotherXmlObject.SomeProperty = "SomeValue";
alert("Another XML object:\n" + anotherXmlObject);
-Manan
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting.
Learn more

