com.aspose.words

Class CustomXmlPart

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

Represents a Custom XML Data Storage Part (custom XML data within a package).

A DOCX or DOC document can contain one or more Custom XML Data Storage parts. Aspose.Words preserves and allows to create and extract Custom XML Data via the Document.CustomXmlParts collection.

Example:

Shows how to create a structured document tag with custom XML data.
Document doc = new Document();

// Construct an XML part that contains data and add it to the document's collection.
// If we enable the "Developer" tab in Microsoft Word,
// we can find elements from this collection in the "XML Mapping Pane", along with a few default elements.
String xmlPartId = UUID.randomUUID().toString();
String xmlPartContent = "<root><text>Hello, World!</text></root>";
CustomXmlPart xmlPart = doc.getCustomXmlParts().add(xmlPartId, xmlPartContent);

Assert.assertEquals(xmlPart.getData(), xmlPartContent.getBytes());
Assert.assertEquals(xmlPart.getId(), xmlPartId);

// Below are two ways to refer to XML parts.
// 1 -  By an index in the custom XML part collection:
Assert.assertEquals(xmlPart, doc.getCustomXmlParts().get(0));

// 2 -  By GUID:
Assert.assertEquals(xmlPart, doc.getCustomXmlParts().getById(xmlPartId));

// Add an XML schema association.
xmlPart.getSchemas().add("http://www.w3.org/2001/XMLSchema");

// Clone a part, and then insert it into the collection.
CustomXmlPart xmlPartClone = xmlPart.deepClone();
xmlPartClone.setId(UUID.randomUUID().toString());
doc.getCustomXmlParts().add(xmlPartClone);

Assert.assertEquals(doc.getCustomXmlParts().getCount(), 2);

// Iterate through the collection and print the contents of each part.
Iterator<CustomXmlPart> enumerator = doc.getCustomXmlParts().iterator();
int index = 0;
while (enumerator.hasNext()) {
    CustomXmlPart customXmlPart = enumerator.next();
    System.out.println(MessageFormat.format("XML part index {0}, ID: {1}", index, customXmlPart.getId()));
    System.out.println(MessageFormat.format("\tContent: {0}", customXmlPart.getData()));
    index++;
}

// Use the "RemoveAt" method to remove the cloned part by index.
doc.getCustomXmlParts().removeAt(1);

Assert.assertEquals(doc.getCustomXmlParts().getCount(), 1);

// Clone the XML parts collection, and then use the "Clear" method to remove all its elements at once.
CustomXmlPartCollection customXmlParts = doc.getCustomXmlParts().deepClone();
customXmlParts.clear();

// Create a structured document tag that will display our part's contents and insert it into the document body.
StructuredDocumentTag tag = new StructuredDocumentTag(doc, SdtType.PLAIN_TEXT, MarkupLevel.BLOCK);
tag.getXmlMapping().setMapping(xmlPart, "/root[1]/text[1]", "");

doc.getFirstSection().getBody().appendChild(tag);

doc.save(getArtifactsDir() + "StructuredDocumentTag.CustomXml.docx");
See Also:
Document.CustomXmlParts, CustomXmlPartCollection

Constructor Summary
 
Property Getters/Setters Summary
byte[]getData()
void
setData(byte[]value)
           Gets or sets the XML content of this Custom XML Data Storage Part.
longgetDataChecksum()
Specifies a cyclic redundancy check (CRC) checksum of the Data content.
java.lang.StringgetId()
void
setId(java.lang.Stringvalue)
           Gets or sets the string that identifies this custom XML part within an OOXML document.
CustomXmlSchemaCollectiongetSchemas()
Specifies the set of XML schemas that are associated with this custom XML part.
 
Method Summary
CustomXmlPartdeepClone()
Makes a "deep enough" copy of the object. Does not duplicate the bytes of the Data value.
 

    • Constructor Detail

      • CustomXmlPart

        public CustomXmlPart()
    • Property Getters/Setters Detail

      • getData/setData

        public byte[] getData() / public void setData(byte[] value)
        
        Gets or sets the XML content of this Custom XML Data Storage Part.

        The default value is an empty byte array. The value cannot be null.

        Example:

        Shows how to create a structured document tag with custom XML data.
        Document doc = new Document();
        
        // Construct an XML part that contains data and add it to the document's collection.
        // If we enable the "Developer" tab in Microsoft Word,
        // we can find elements from this collection in the "XML Mapping Pane", along with a few default elements.
        String xmlPartId = UUID.randomUUID().toString();
        String xmlPartContent = "<root><text>Hello, World!</text></root>";
        CustomXmlPart xmlPart = doc.getCustomXmlParts().add(xmlPartId, xmlPartContent);
        
        Assert.assertEquals(xmlPart.getData(), xmlPartContent.getBytes());
        Assert.assertEquals(xmlPart.getId(), xmlPartId);
        
        // Below are two ways to refer to XML parts.
        // 1 -  By an index in the custom XML part collection:
        Assert.assertEquals(xmlPart, doc.getCustomXmlParts().get(0));
        
        // 2 -  By GUID:
        Assert.assertEquals(xmlPart, doc.getCustomXmlParts().getById(xmlPartId));
        
        // Add an XML schema association.
        xmlPart.getSchemas().add("http://www.w3.org/2001/XMLSchema");
        
        // Clone a part, and then insert it into the collection.
        CustomXmlPart xmlPartClone = xmlPart.deepClone();
        xmlPartClone.setId(UUID.randomUUID().toString());
        doc.getCustomXmlParts().add(xmlPartClone);
        
        Assert.assertEquals(doc.getCustomXmlParts().getCount(), 2);
        
        // Iterate through the collection and print the contents of each part.
        Iterator<CustomXmlPart> enumerator = doc.getCustomXmlParts().iterator();
        int index = 0;
        while (enumerator.hasNext()) {
            CustomXmlPart customXmlPart = enumerator.next();
            System.out.println(MessageFormat.format("XML part index {0}, ID: {1}", index, customXmlPart.getId()));
            System.out.println(MessageFormat.format("\tContent: {0}", customXmlPart.getData()));
            index++;
        }
        
        // Use the "RemoveAt" method to remove the cloned part by index.
        doc.getCustomXmlParts().removeAt(1);
        
        Assert.assertEquals(doc.getCustomXmlParts().getCount(), 1);
        
        // Clone the XML parts collection, and then use the "Clear" method to remove all its elements at once.
        CustomXmlPartCollection customXmlParts = doc.getCustomXmlParts().deepClone();
        customXmlParts.clear();
        
        // Create a structured document tag that will display our part's contents and insert it into the document body.
        StructuredDocumentTag tag = new StructuredDocumentTag(doc, SdtType.PLAIN_TEXT, MarkupLevel.BLOCK);
        tag.getXmlMapping().setMapping(xmlPart, "/root[1]/text[1]", "");
        
        doc.getFirstSection().getBody().appendChild(tag);
        
        doc.save(getArtifactsDir() + "StructuredDocumentTag.CustomXml.docx");
      • getDataChecksum

        public long getDataChecksum()
        
        Specifies a cyclic redundancy check (CRC) checksum of the Data content.

        Example:

        Shows how the checksum is calculated in a runtime.
        Document doc = new Document();
        
        StructuredDocumentTag richText = new StructuredDocumentTag(doc, SdtType.RICH_TEXT, MarkupLevel.BLOCK);
        doc.getFirstSection().getBody().appendChild(richText);
        
        // The checksum is read-only and computed using the data of the corresponding custom XML data part.
        richText.getXmlMapping().setMapping(doc.getCustomXmlParts().add(UUID.randomUUID().toString(),
                "<root><text>ContentControl</text></root>"), "/root/text", "");
        
        long checksum = richText.getXmlMapping().getCustomXmlPart().getDataChecksum();
        System.out.println(checksum);
        
        richText.getXmlMapping().setMapping(doc.getCustomXmlParts().add(UUID.randomUUID().toString(),
                "<root><text>Updated ContentControl</text></root>"), "/root/text", "");
        
        long updatedChecksum = richText.getXmlMapping().getCustomXmlPart().getDataChecksum();
        System.out.println(updatedChecksum);
        
        // We changed the XmlPart of the tag, and the checksum was updated at runtime.
        Assert.assertNotEquals(checksum, updatedChecksum);
      • getId/setId

        public java.lang.String getId() / public void setId(java.lang.String value)
        
        Gets or sets the string that identifies this custom XML part within an OOXML document.

        ISO/IEC 29500 specifies that this value is a GUID, but old versions of Microsoft Word allowed any string here. Aspose.Words does the same for ECMA-376 format. But note, that Microsoft Word Online fails to open a document created with a non-GUID value. So, a GUID is preferred value for this property.

        A valid value must be an identifier that is unique among all custom XML data parts in this document.

        The default value is an empty string. The value cannot be null.

        Example:

        Shows how to create a structured document tag with custom XML data.
        Document doc = new Document();
        
        // Construct an XML part that contains data and add it to the document's collection.
        // If we enable the "Developer" tab in Microsoft Word,
        // we can find elements from this collection in the "XML Mapping Pane", along with a few default elements.
        String xmlPartId = UUID.randomUUID().toString();
        String xmlPartContent = "<root><text>Hello, World!</text></root>";
        CustomXmlPart xmlPart = doc.getCustomXmlParts().add(xmlPartId, xmlPartContent);
        
        Assert.assertEquals(xmlPart.getData(), xmlPartContent.getBytes());
        Assert.assertEquals(xmlPart.getId(), xmlPartId);
        
        // Below are two ways to refer to XML parts.
        // 1 -  By an index in the custom XML part collection:
        Assert.assertEquals(xmlPart, doc.getCustomXmlParts().get(0));
        
        // 2 -  By GUID:
        Assert.assertEquals(xmlPart, doc.getCustomXmlParts().getById(xmlPartId));
        
        // Add an XML schema association.
        xmlPart.getSchemas().add("http://www.w3.org/2001/XMLSchema");
        
        // Clone a part, and then insert it into the collection.
        CustomXmlPart xmlPartClone = xmlPart.deepClone();
        xmlPartClone.setId(UUID.randomUUID().toString());
        doc.getCustomXmlParts().add(xmlPartClone);
        
        Assert.assertEquals(doc.getCustomXmlParts().getCount(), 2);
        
        // Iterate through the collection and print the contents of each part.
        Iterator<CustomXmlPart> enumerator = doc.getCustomXmlParts().iterator();
        int index = 0;
        while (enumerator.hasNext()) {
            CustomXmlPart customXmlPart = enumerator.next();
            System.out.println(MessageFormat.format("XML part index {0}, ID: {1}", index, customXmlPart.getId()));
            System.out.println(MessageFormat.format("\tContent: {0}", customXmlPart.getData()));
            index++;
        }
        
        // Use the "RemoveAt" method to remove the cloned part by index.
        doc.getCustomXmlParts().removeAt(1);
        
        Assert.assertEquals(doc.getCustomXmlParts().getCount(), 1);
        
        // Clone the XML parts collection, and then use the "Clear" method to remove all its elements at once.
        CustomXmlPartCollection customXmlParts = doc.getCustomXmlParts().deepClone();
        customXmlParts.clear();
        
        // Create a structured document tag that will display our part's contents and insert it into the document body.
        StructuredDocumentTag tag = new StructuredDocumentTag(doc, SdtType.PLAIN_TEXT, MarkupLevel.BLOCK);
        tag.getXmlMapping().setMapping(xmlPart, "/root[1]/text[1]", "");
        
        doc.getFirstSection().getBody().appendChild(tag);
        
        doc.save(getArtifactsDir() + "StructuredDocumentTag.CustomXml.docx");
      • getSchemas

        public CustomXmlSchemaCollection getSchemas()
        
        Specifies the set of XML schemas that are associated with this custom XML part.

        Example:

        Shows how to create a structured document tag with custom XML data.
        Document doc = new Document();
        
        // Construct an XML part that contains data and add it to the document's collection.
        // If we enable the "Developer" tab in Microsoft Word,
        // we can find elements from this collection in the "XML Mapping Pane", along with a few default elements.
        String xmlPartId = UUID.randomUUID().toString();
        String xmlPartContent = "<root><text>Hello, World!</text></root>";
        CustomXmlPart xmlPart = doc.getCustomXmlParts().add(xmlPartId, xmlPartContent);
        
        Assert.assertEquals(xmlPart.getData(), xmlPartContent.getBytes());
        Assert.assertEquals(xmlPart.getId(), xmlPartId);
        
        // Below are two ways to refer to XML parts.
        // 1 -  By an index in the custom XML part collection:
        Assert.assertEquals(xmlPart, doc.getCustomXmlParts().get(0));
        
        // 2 -  By GUID:
        Assert.assertEquals(xmlPart, doc.getCustomXmlParts().getById(xmlPartId));
        
        // Add an XML schema association.
        xmlPart.getSchemas().add("http://www.w3.org/2001/XMLSchema");
        
        // Clone a part, and then insert it into the collection.
        CustomXmlPart xmlPartClone = xmlPart.deepClone();
        xmlPartClone.setId(UUID.randomUUID().toString());
        doc.getCustomXmlParts().add(xmlPartClone);
        
        Assert.assertEquals(doc.getCustomXmlParts().getCount(), 2);
        
        // Iterate through the collection and print the contents of each part.
        Iterator<CustomXmlPart> enumerator = doc.getCustomXmlParts().iterator();
        int index = 0;
        while (enumerator.hasNext()) {
            CustomXmlPart customXmlPart = enumerator.next();
            System.out.println(MessageFormat.format("XML part index {0}, ID: {1}", index, customXmlPart.getId()));
            System.out.println(MessageFormat.format("\tContent: {0}", customXmlPart.getData()));
            index++;
        }
        
        // Use the "RemoveAt" method to remove the cloned part by index.
        doc.getCustomXmlParts().removeAt(1);
        
        Assert.assertEquals(doc.getCustomXmlParts().getCount(), 1);
        
        // Clone the XML parts collection, and then use the "Clear" method to remove all its elements at once.
        CustomXmlPartCollection customXmlParts = doc.getCustomXmlParts().deepClone();
        customXmlParts.clear();
        
        // Create a structured document tag that will display our part's contents and insert it into the document body.
        StructuredDocumentTag tag = new StructuredDocumentTag(doc, SdtType.PLAIN_TEXT, MarkupLevel.BLOCK);
        tag.getXmlMapping().setMapping(xmlPart, "/root[1]/text[1]", "");
        
        doc.getFirstSection().getBody().appendChild(tag);
        
        doc.save(getArtifactsDir() + "StructuredDocumentTag.CustomXml.docx");
    • Method Detail

      • deepClone

        public CustomXmlPart deepClone()
        Makes a "deep enough" copy of the object. Does not duplicate the bytes of the Data value.

        Example:

        Shows how to create a structured document tag with custom XML data.
        Document doc = new Document();
        
        // Construct an XML part that contains data and add it to the document's collection.
        // If we enable the "Developer" tab in Microsoft Word,
        // we can find elements from this collection in the "XML Mapping Pane", along with a few default elements.
        String xmlPartId = UUID.randomUUID().toString();
        String xmlPartContent = "<root><text>Hello, World!</text></root>";
        CustomXmlPart xmlPart = doc.getCustomXmlParts().add(xmlPartId, xmlPartContent);
        
        Assert.assertEquals(xmlPart.getData(), xmlPartContent.getBytes());
        Assert.assertEquals(xmlPart.getId(), xmlPartId);
        
        // Below are two ways to refer to XML parts.
        // 1 -  By an index in the custom XML part collection:
        Assert.assertEquals(xmlPart, doc.getCustomXmlParts().get(0));
        
        // 2 -  By GUID:
        Assert.assertEquals(xmlPart, doc.getCustomXmlParts().getById(xmlPartId));
        
        // Add an XML schema association.
        xmlPart.getSchemas().add("http://www.w3.org/2001/XMLSchema");
        
        // Clone a part, and then insert it into the collection.
        CustomXmlPart xmlPartClone = xmlPart.deepClone();
        xmlPartClone.setId(UUID.randomUUID().toString());
        doc.getCustomXmlParts().add(xmlPartClone);
        
        Assert.assertEquals(doc.getCustomXmlParts().getCount(), 2);
        
        // Iterate through the collection and print the contents of each part.
        Iterator<CustomXmlPart> enumerator = doc.getCustomXmlParts().iterator();
        int index = 0;
        while (enumerator.hasNext()) {
            CustomXmlPart customXmlPart = enumerator.next();
            System.out.println(MessageFormat.format("XML part index {0}, ID: {1}", index, customXmlPart.getId()));
            System.out.println(MessageFormat.format("\tContent: {0}", customXmlPart.getData()));
            index++;
        }
        
        // Use the "RemoveAt" method to remove the cloned part by index.
        doc.getCustomXmlParts().removeAt(1);
        
        Assert.assertEquals(doc.getCustomXmlParts().getCount(), 1);
        
        // Clone the XML parts collection, and then use the "Clear" method to remove all its elements at once.
        CustomXmlPartCollection customXmlParts = doc.getCustomXmlParts().deepClone();
        customXmlParts.clear();
        
        // Create a structured document tag that will display our part's contents and insert it into the document body.
        StructuredDocumentTag tag = new StructuredDocumentTag(doc, SdtType.PLAIN_TEXT, MarkupLevel.BLOCK);
        tag.getXmlMapping().setMapping(xmlPart, "/root[1]/text[1]", "");
        
        doc.getFirstSection().getBody().appendChild(tag);
        
        doc.save(getArtifactsDir() + "StructuredDocumentTag.CustomXml.docx");