Adobe 65009333 Scripting Guide - Page 79

Importing XML, Creating an XML tag, Loading XML tags

Page 79 highlights

XML Scripting XML Elements 79 Importing XML Once you set the XML-import preferences the way you want them, you can import an XML file, as shown in the following script fragment (from the ImportXML tutorial script): myDocument.importXML(File("/c/completeDocument.xml")); When you need to import the contents of an XML file into a specific XML element, use the importXML method of the XML element, rather than the corresponding method of the document. See the following script fragment (from the ImportXMLIntoElement tutorial script): var myXMLTag = myDocument.xmlTags.add("xml_element"); var myXMLElement = myDocument.xmlElements.item(0).xmlElements.add(myXMLTag); //Import into the new XML element. myXMLElement.importXML(File("/c/completeDocument.xml")); You also can set the importToSelected property of the xmlImportPreferences object to true, then select the XML element, and then import the XML file, as shown in the following script fragment (from the ImportXMLIntoSelectedXMLElement tutorial script): var myXMLTag = myDocument.xmlTags.add("xml_element"); var myXMLElement = myDocument.xmlElements.item(0).xmlElements.add(myXMLTag); myDocument.select(myXMLElement); myDocument.xmlImportPreferences.importToSelected = true; //Import into the selected XML element. myDocument.importXML(File("/c/test.xml")); Creating an XML tag XML tags are the names of XML elements you want to create in a document. When you import XML, the element names in the XML file are added to the list of XML tags in the document. You also can create XML tags directly, as shown in the following script fragment (from the MakeXMLTags tutorial script): //You can create an XML tag without specifying a color for the tag. var myXMLTagA = myDocument.xmlTags.add("XML_tag_A"); //You can define the highlight color of the XML tag using the UIColors enumeration... var myXMLTagB = myDocument.xmlTags.add("XML_tag_B", UIColors.gray); //...or you can provide an RGB array to set the color of the tag. var myXMLTagC = myDocument.xmlTags.add("XML_tag_C", [0, 92, 128]); Loading XML tags You can import XML tags from an XML file without importing the XML contents of the file. You might want to do this to work out a tag-to-style or style-to-tag mapping before importing the XML data., as shown in the following script fragment (from the LoadXMLTags tutorial script): myDocument.loadXMLTags(File("/c/test.xml")); Saving XML tags Just as you can load XML tags from a file, you can save XML tags to a file, as shown in the following script. When you do this, only the tags themselves are saved in the XML file; document data is not included. As you would expect, this process is much faster than exporting XML, and the resulting file is much smaller. The following sample script shows how to save XML tags (for the complete script, see SaveXMLTags):

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88

XML
Scripting XML Elements
79
Importing XML
Once you set the XML-import preferences the way you want them, you can import an XML file, as shown in
the following script fragment (from the ImportXML tutorial script):
myDocument.importXML(File("/c/completeDocument.xml"));
When you need to import the contents of an XML file into a specific XML element, use the importXML
method of the XML element, rather than the corresponding method of the document. See the following
script fragment (from the ImportXMLIntoElement tutorial script):
var myXMLTag = myDocument.xmlTags.add("xml_element");
var myXMLElement = myDocument.xmlElements.item(0).xmlElements.add(myXMLTag);
//Import into the new XML element.
myXMLElement.importXML(File("/c/completeDocument.xml"));
You also can set the
importToSelected
property of the
xmlImportPreferences
object to true, then
select the XML element, and then import the XML file, as shown in the following script fragment (from the
ImportXMLIntoSelectedXMLElement tutorial script):
var myXMLTag = myDocument.xmlTags.add("xml_element");
var myXMLElement = myDocument.xmlElements.item(0).xmlElements.add(myXMLTag);
myDocument.select(myXMLElement);
myDocument.xmlImportPreferences.importToSelected = true;
//Import into the selected XML element.
myDocument.importXML(File("/c/test.xml"));
Creating an XML tag
XML tags are the names of XML elements you want to create in a document. When you import XML, the
element names in the XML file are added to the list of XML tags in the document. You also can create XML
tags directly, as shown in the following script fragment (from the MakeXMLTags tutorial script):
//You can create an XML tag without specifying a color for the tag.
var myXMLTagA = myDocument.xmlTags.add("XML_tag_A");
//You can define the highlight color of the XML tag using the UIColors enumeration...
var myXMLTagB = myDocument.xmlTags.add("XML_tag_B", UIColors.gray);
//...or you can provide an RGB array to set the color of the tag.
var myXMLTagC = myDocument.xmlTags.add("XML_tag_C", [0, 92, 128]);
Loading XML tags
You can import XML tags from an XML file without importing the XML contents of the file. You might want
to do this to work out a tag-to-style or style-to-tag mapping before importing the XML data., as shown in
the following script fragment (from the LoadXMLTags tutorial script):
myDocument.loadXMLTags(File("/c/test.xml"));
Saving XML tags
Just as you can load XML tags from a file, you can save XML tags to a file, as shown in the following script.
When you do this, only the tags themselves are saved in the XML file; document data is not included. As
you would expect, this process is much faster than exporting XML, and the resulting file is much smaller.
The following sample script shows how to save XML tags (for the complete script, see SaveXMLTags):