com.aspose.words

Class CustomXmlProperty

  • java.lang.Object
    • com.aspose.words.CustomXmlProperty
  • All Implemented Interfaces:
    java.lang.Cloneable
    public class CustomXmlProperty 
    extends java.lang.Object

Represents a single custom XML attribute or a smart tag property.

Used as an item of a CustomXmlPropertyCollection collection.

Example:

Shows how to create smart tags.
public void smartTags() throws Exception {
    Document doc = new Document();
    SmartTag smartTag = new SmartTag(doc);
    smartTag.setElement("date");

    // Specify a date and set smart tag properties accordingly
    smartTag.appendChild(new Run(doc, "May 29, 2019"));

    smartTag.getProperties().add(new CustomXmlProperty("Day", "", "29"));
    smartTag.getProperties().add(new CustomXmlProperty("Month", "", "5"));
    smartTag.getProperties().add(new CustomXmlProperty("Year", "", "2019"));

    // Set the smart tag's uri to the default
    smartTag.setUri("urn:schemas-microsoft-com:office:smarttags");

    doc.getFirstSection().getBody().getFirstParagraph().appendChild(smartTag);
    doc.getFirstSection().getBody().getFirstParagraph().appendChild(new Run(doc, " is a date. "));

    // Create and add one more smart tag, this time for a financial symbol
    smartTag = new SmartTag(doc);
    smartTag.setElement("stockticker");
    smartTag.setUri("urn:schemas-microsoft-com:office:smarttags");

    smartTag.appendChild(new Run(doc, "MSFT"));

    doc.getFirstSection().getBody().getFirstParagraph().appendChild(smartTag);
    doc.getFirstSection().getBody().getFirstParagraph().appendChild(new Run(doc, " is a stock ticker."));

    // Print all the smart tags in our document with a document visitor
    doc.accept(new SmartTagVisitor());

    doc.save(getArtifactsDir() + "StructuredDocumentTag.SmartTags.docx");
}

/// <summary>
/// DocumentVisitor implementation that prints smart tags and their contents
/// </summary>
private static class SmartTagVisitor extends DocumentVisitor {
    /// <summary>
    /// Called when a SmartTag node is encountered in the document.
    /// </summary>
    public int visitSmartTagStart(SmartTag smartTag) {
        System.out.println(MessageFormat.format("Smart tag type: {0}", smartTag.getElement()));
        return VisitorAction.CONTINUE;
    }

    /// <summary>
    /// Called when the visiting of a SmartTag node is ended.
    /// </summary>
    public int visitSmartTagEnd(SmartTag smartTag) throws Exception {
        System.out.println(MessageFormat.format("\tContents: \"{0}\"", smartTag.toString(SaveFormat.TEXT)));

        if (smartTag.getProperties().getCount() == 0) {
            System.out.println("\tContains no properties");

        } else {
            System.out.println("\tProperties: ");
            String[] properties = new String[smartTag.getProperties().getCount()];
            int index = 0;

            for (CustomXmlProperty cxp : smartTag.getProperties()) {
                properties[index++] = MessageFormat.format("\"{0}\" = \"{1}\"", cxp.getName(), cxp.getValue());
            }

            System.out.println(String.join(", ", properties));
        }

        return VisitorAction.CONTINUE;
    }
}

Constructor Summary
CustomXmlProperty(java.lang.Stringname, java.lang.Stringuri, java.lang.Stringvalue)
Initializes a new instance of this class.
 
Property Getters/Setters Summary
java.lang.StringgetName()
Specifies the name of the custom XML attribute or smart tag property.
java.lang.StringgetUri()
void
setUri(java.lang.Stringvalue)
           Gets or sets the namespace URI of the custom XML attribute or smart tag property.
java.lang.StringgetValue()
void
setValue(java.lang.Stringvalue)
           Gets or sets the value of the custom XML attribute or smart tag property.
 

    • Constructor Detail

      • CustomXmlProperty

        public CustomXmlProperty(java.lang.String name, java.lang.String uri, java.lang.String value)
        Initializes a new instance of this class.
        Parameters:
        name - The name of the property. Cannot be null.
        uri - The namespace URI of the property. Cannot be null.
        value - The value of the property. Cannot be null.

        Example:

        Shows how to create smart tags.
        public void smartTags() throws Exception {
            Document doc = new Document();
            SmartTag smartTag = new SmartTag(doc);
            smartTag.setElement("date");
        
            // Specify a date and set smart tag properties accordingly
            smartTag.appendChild(new Run(doc, "May 29, 2019"));
        
            smartTag.getProperties().add(new CustomXmlProperty("Day", "", "29"));
            smartTag.getProperties().add(new CustomXmlProperty("Month", "", "5"));
            smartTag.getProperties().add(new CustomXmlProperty("Year", "", "2019"));
        
            // Set the smart tag's uri to the default
            smartTag.setUri("urn:schemas-microsoft-com:office:smarttags");
        
            doc.getFirstSection().getBody().getFirstParagraph().appendChild(smartTag);
            doc.getFirstSection().getBody().getFirstParagraph().appendChild(new Run(doc, " is a date. "));
        
            // Create and add one more smart tag, this time for a financial symbol
            smartTag = new SmartTag(doc);
            smartTag.setElement("stockticker");
            smartTag.setUri("urn:schemas-microsoft-com:office:smarttags");
        
            smartTag.appendChild(new Run(doc, "MSFT"));
        
            doc.getFirstSection().getBody().getFirstParagraph().appendChild(smartTag);
            doc.getFirstSection().getBody().getFirstParagraph().appendChild(new Run(doc, " is a stock ticker."));
        
            // Print all the smart tags in our document with a document visitor
            doc.accept(new SmartTagVisitor());
        
            doc.save(getArtifactsDir() + "StructuredDocumentTag.SmartTags.docx");
        }
        
        /// <summary>
        /// DocumentVisitor implementation that prints smart tags and their contents
        /// </summary>
        private static class SmartTagVisitor extends DocumentVisitor {
            /// <summary>
            /// Called when a SmartTag node is encountered in the document.
            /// </summary>
            public int visitSmartTagStart(SmartTag smartTag) {
                System.out.println(MessageFormat.format("Smart tag type: {0}", smartTag.getElement()));
                return VisitorAction.CONTINUE;
            }
        
            /// <summary>
            /// Called when the visiting of a SmartTag node is ended.
            /// </summary>
            public int visitSmartTagEnd(SmartTag smartTag) throws Exception {
                System.out.println(MessageFormat.format("\tContents: \"{0}\"", smartTag.toString(SaveFormat.TEXT)));
        
                if (smartTag.getProperties().getCount() == 0) {
                    System.out.println("\tContains no properties");
        
                } else {
                    System.out.println("\tProperties: ");
                    String[] properties = new String[smartTag.getProperties().getCount()];
                    int index = 0;
        
                    for (CustomXmlProperty cxp : smartTag.getProperties()) {
                        properties[index++] = MessageFormat.format("\"{0}\" = \"{1}\"", cxp.getName(), cxp.getValue());
                    }
        
                    System.out.println(String.join(", ", properties));
                }
        
                return VisitorAction.CONTINUE;
            }
        }
    • Property Getters/Setters Detail

      • getName

        public java.lang.String getName()
        
        Specifies the name of the custom XML attribute or smart tag property.

        Cannot be null.

        Default is empty string.

        Example:

        Shows how to create smart tags.
        public void smartTags() throws Exception {
            Document doc = new Document();
            SmartTag smartTag = new SmartTag(doc);
            smartTag.setElement("date");
        
            // Specify a date and set smart tag properties accordingly
            smartTag.appendChild(new Run(doc, "May 29, 2019"));
        
            smartTag.getProperties().add(new CustomXmlProperty("Day", "", "29"));
            smartTag.getProperties().add(new CustomXmlProperty("Month", "", "5"));
            smartTag.getProperties().add(new CustomXmlProperty("Year", "", "2019"));
        
            // Set the smart tag's uri to the default
            smartTag.setUri("urn:schemas-microsoft-com:office:smarttags");
        
            doc.getFirstSection().getBody().getFirstParagraph().appendChild(smartTag);
            doc.getFirstSection().getBody().getFirstParagraph().appendChild(new Run(doc, " is a date. "));
        
            // Create and add one more smart tag, this time for a financial symbol
            smartTag = new SmartTag(doc);
            smartTag.setElement("stockticker");
            smartTag.setUri("urn:schemas-microsoft-com:office:smarttags");
        
            smartTag.appendChild(new Run(doc, "MSFT"));
        
            doc.getFirstSection().getBody().getFirstParagraph().appendChild(smartTag);
            doc.getFirstSection().getBody().getFirstParagraph().appendChild(new Run(doc, " is a stock ticker."));
        
            // Print all the smart tags in our document with a document visitor
            doc.accept(new SmartTagVisitor());
        
            doc.save(getArtifactsDir() + "StructuredDocumentTag.SmartTags.docx");
        }
        
        /// <summary>
        /// DocumentVisitor implementation that prints smart tags and their contents
        /// </summary>
        private static class SmartTagVisitor extends DocumentVisitor {
            /// <summary>
            /// Called when a SmartTag node is encountered in the document.
            /// </summary>
            public int visitSmartTagStart(SmartTag smartTag) {
                System.out.println(MessageFormat.format("Smart tag type: {0}", smartTag.getElement()));
                return VisitorAction.CONTINUE;
            }
        
            /// <summary>
            /// Called when the visiting of a SmartTag node is ended.
            /// </summary>
            public int visitSmartTagEnd(SmartTag smartTag) throws Exception {
                System.out.println(MessageFormat.format("\tContents: \"{0}\"", smartTag.toString(SaveFormat.TEXT)));
        
                if (smartTag.getProperties().getCount() == 0) {
                    System.out.println("\tContains no properties");
        
                } else {
                    System.out.println("\tProperties: ");
                    String[] properties = new String[smartTag.getProperties().getCount()];
                    int index = 0;
        
                    for (CustomXmlProperty cxp : smartTag.getProperties()) {
                        properties[index++] = MessageFormat.format("\"{0}\" = \"{1}\"", cxp.getName(), cxp.getValue());
                    }
        
                    System.out.println(String.join(", ", properties));
                }
        
                return VisitorAction.CONTINUE;
            }
        }
      • getUri/setUri

        public java.lang.String getUri() / public void setUri(java.lang.String value)
        
        Gets or sets the namespace URI of the custom XML attribute or smart tag property.

        Cannot be null.

        Default is empty string.

        Example:

        Shows how to work with smart tag properties to get in depth information about smart tags.
        // Open a document that contains smart tags and their collection
        Document doc = new Document(getMyDir() + "SmartTags.doc");
        
        // Smart tags are an older Microsoft Word feature that can automatically detect and tag
        // any parts of the text that it registers as commonly used information objects such as names, addresses, stock tickers, dates etc
        // In Word 2003, smart tags can be turned on in Tools > AutoCorrect options... > SmartTags tab
        // In our input document there are three objects that were registered as smart tags, but since they can be nested, we have 8 in this collection
        NodeCollection smartTags = doc.getChildNodes(NodeType.SMART_TAG, true);
        Assert.assertEquals(smartTags.getCount(), 8);
        
        // The last smart tag is of the "Date" type, which we will retrieve here
        SmartTag smartTag = (SmartTag) smartTags.get(7);
        
        // The Properties attribute, for some smart tags, elaborates on the text object that Word picked up as a smart tag
        // In the case of our "Date" smart tag, its properties will let us know the year, month and day within the smart tag
        CustomXmlPropertyCollection properties = smartTag.getProperties();
        
        // We can enumerate over the collection and print the aforementioned properties to the console
        Assert.assertEquals(properties.getCount(), 4);
        
        Iterator<CustomXmlProperty> enumerator = properties.iterator();
        try {
            while (enumerator.hasNext()) {
                CustomXmlProperty customXmlProperty = enumerator.next();
        
                System.out.println(MessageFormat.format("Property name: {0}, value: {1}", customXmlProperty.getName(), customXmlProperty.getValue()));
                Assert.assertEquals(enumerator.next().getUri(), "");
            }
        } finally {
            if (enumerator != null) enumerator.remove();
        }
        
        // We can also access the elements in various ways, including as a key-value pair
        Assert.assertTrue(properties.contains("Day"));
        Assert.assertEquals(properties.get("Day").getValue(), "22");
        Assert.assertEquals(properties.get(2).getValue(), "2003");
        Assert.assertEquals(properties.indexOfKey("Month"), 1);
        
        // We can also remove elements by name, index or clear the collection entirely
        properties.removeAt(3);
        properties.remove("Year");
        Assert.assertEquals((properties.getCount()), 2);
        
        properties.clear();
        Assert.assertEquals((properties.getCount()), 0);
        
        // We can remove the entire smart tag like this
        smartTag.remove();
      • getValue/setValue

        public java.lang.String getValue() / public void setValue(java.lang.String value)
        
        Gets or sets the value of the custom XML attribute or smart tag property.

        Cannot be null.

        Default is empty string.

        Example:

        Shows how to create smart tags.
        public void smartTags() throws Exception {
            Document doc = new Document();
            SmartTag smartTag = new SmartTag(doc);
            smartTag.setElement("date");
        
            // Specify a date and set smart tag properties accordingly
            smartTag.appendChild(new Run(doc, "May 29, 2019"));
        
            smartTag.getProperties().add(new CustomXmlProperty("Day", "", "29"));
            smartTag.getProperties().add(new CustomXmlProperty("Month", "", "5"));
            smartTag.getProperties().add(new CustomXmlProperty("Year", "", "2019"));
        
            // Set the smart tag's uri to the default
            smartTag.setUri("urn:schemas-microsoft-com:office:smarttags");
        
            doc.getFirstSection().getBody().getFirstParagraph().appendChild(smartTag);
            doc.getFirstSection().getBody().getFirstParagraph().appendChild(new Run(doc, " is a date. "));
        
            // Create and add one more smart tag, this time for a financial symbol
            smartTag = new SmartTag(doc);
            smartTag.setElement("stockticker");
            smartTag.setUri("urn:schemas-microsoft-com:office:smarttags");
        
            smartTag.appendChild(new Run(doc, "MSFT"));
        
            doc.getFirstSection().getBody().getFirstParagraph().appendChild(smartTag);
            doc.getFirstSection().getBody().getFirstParagraph().appendChild(new Run(doc, " is a stock ticker."));
        
            // Print all the smart tags in our document with a document visitor
            doc.accept(new SmartTagVisitor());
        
            doc.save(getArtifactsDir() + "StructuredDocumentTag.SmartTags.docx");
        }
        
        /// <summary>
        /// DocumentVisitor implementation that prints smart tags and their contents
        /// </summary>
        private static class SmartTagVisitor extends DocumentVisitor {
            /// <summary>
            /// Called when a SmartTag node is encountered in the document.
            /// </summary>
            public int visitSmartTagStart(SmartTag smartTag) {
                System.out.println(MessageFormat.format("Smart tag type: {0}", smartTag.getElement()));
                return VisitorAction.CONTINUE;
            }
        
            /// <summary>
            /// Called when the visiting of a SmartTag node is ended.
            /// </summary>
            public int visitSmartTagEnd(SmartTag smartTag) throws Exception {
                System.out.println(MessageFormat.format("\tContents: \"{0}\"", smartTag.toString(SaveFormat.TEXT)));
        
                if (smartTag.getProperties().getCount() == 0) {
                    System.out.println("\tContains no properties");
        
                } else {
                    System.out.println("\tProperties: ");
                    String[] properties = new String[smartTag.getProperties().getCount()];
                    int index = 0;
        
                    for (CustomXmlProperty cxp : smartTag.getProperties()) {
                        properties[index++] = MessageFormat.format("\"{0}\" = \"{1}\"", cxp.getName(), cxp.getValue());
                    }
        
                    System.out.println(String.join(", ", properties));
                }
        
                return VisitorAction.CONTINUE;
            }
        }