java.lang.Object
com.aspose.words.CustomXmlPart
public class CustomXmlPart
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 Example:
Document doc = new Document();
// Construct an XML part that contains data and add it to the document's collection
// Once the "Developer" tab in Mircosoft Word is enabled,
// we can find elements from this collection as well as a couple defaults in the "XML Mapping Pane"
String xmlPartId = UUID.randomUUID().toString();
String xmlPartContent = "<root><text>Hello, World!</text></root>";
CustomXmlPart xmlPart = doc.getCustomXmlParts().add(xmlPartId, xmlPartContent);
// The data we entered resides in these variables
Assert.assertEquals(xmlPart.getData(), xmlPartContent.getBytes());
Assert.assertEquals(xmlPart.getId(), xmlPartId);
// XML parts can be referenced by collection index or GUID
Assert.assertEquals(doc.getCustomXmlParts().get(0), xmlPart);
Assert.assertEquals(doc.getCustomXmlParts().getById(xmlPartId), xmlPart);
// Once the part is created, we can add XML schema associations like this
xmlPart.getSchemas().add("http://www.w3.org/2001/XMLSchema");
// We can also clone parts and insert them into the collection directly
CustomXmlPart xmlPartClone = xmlPart.deepClone();
xmlPartClone.setId(UUID.randomUUID().toString());
doc.getCustomXmlParts().add(xmlPartClone);
Assert.assertEquals(doc.getCustomXmlParts().getCount(), 2);
// Iterate through collection with an enumerator 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++;
}
// XML parts can be removed by index
doc.getCustomXmlParts().removeAt(1);
Assert.assertEquals(doc.getCustomXmlParts().getCount(), 1);
// The XML part collection itself can be cloned also
CustomXmlPartCollection customXmlParts = doc.getCustomXmlParts().deepClone();
// And all elements can be cleared like this
customXmlParts.clear();
// Create a StructuredDocumentTag that will display the contents of our part,
// insert it into the document and save the document
StructuredDocumentTag sdt = new StructuredDocumentTag(doc, SdtType.PLAIN_TEXT, MarkupLevel.BLOCK);
sdt.getXmlMapping().setMapping(xmlPart, "/root[1]/text[1]", "");
doc.getFirstSection().getBody().appendChild(sdt);
doc.save(getArtifactsDir() + "SDT.CustomXml.docx");
| Constructor Summary |
|---|
CustomXmlPart()
|
| Property Getters/Setters Summary | ||
|---|---|---|
byte[] | getData() | |
void | setData(byte[] value) | |
| Gets or sets the XML content of this Custom XML Data Storage Part. | ||
java.lang.String | getId() | |
void | setId(java.lang.String value) | |
| Gets or sets the string that identifies this custom XML part within an OOXML document. | ||
CustomXmlSchemaCollection | getSchemas() | |
| Specifies the set of XML schemas that are associated with this custom XML part. | ||
| Method Summary | ||
|---|---|---|
CustomXmlPart | deepClone() | |
|
Makes a "deep enough" copy of the object.
Does not duplicate the bytes of the |
||
| Constructor Detail |
|---|
public CustomXmlPart()
| Property Getters/Setters Detail |
|---|
getData/setData | |
public byte[] getData() / public void setData(byte[] value) | |
The default value is an empty byte array. The value cannot be null.
Example:
Shows how to create structured document tag with a custom XML data.
Document doc = new Document();
// Construct an XML part that contains data and add it to the document's collection
// Once the "Developer" tab in Mircosoft Word is enabled,
// we can find elements from this collection as well as a couple defaults in the "XML Mapping Pane"
String xmlPartId = UUID.randomUUID().toString();
String xmlPartContent = "<root><text>Hello, World!</text></root>";
CustomXmlPart xmlPart = doc.getCustomXmlParts().add(xmlPartId, xmlPartContent);
// The data we entered resides in these variables
Assert.assertEquals(xmlPart.getData(), xmlPartContent.getBytes());
Assert.assertEquals(xmlPart.getId(), xmlPartId);
// XML parts can be referenced by collection index or GUID
Assert.assertEquals(doc.getCustomXmlParts().get(0), xmlPart);
Assert.assertEquals(doc.getCustomXmlParts().getById(xmlPartId), xmlPart);
// Once the part is created, we can add XML schema associations like this
xmlPart.getSchemas().add("http://www.w3.org/2001/XMLSchema");
// We can also clone parts and insert them into the collection directly
CustomXmlPart xmlPartClone = xmlPart.deepClone();
xmlPartClone.setId(UUID.randomUUID().toString());
doc.getCustomXmlParts().add(xmlPartClone);
Assert.assertEquals(doc.getCustomXmlParts().getCount(), 2);
// Iterate through collection with an enumerator 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++;
}
// XML parts can be removed by index
doc.getCustomXmlParts().removeAt(1);
Assert.assertEquals(doc.getCustomXmlParts().getCount(), 1);
// The XML part collection itself can be cloned also
CustomXmlPartCollection customXmlParts = doc.getCustomXmlParts().deepClone();
// And all elements can be cleared like this
customXmlParts.clear();
// Create a StructuredDocumentTag that will display the contents of our part,
// insert it into the document and save the document
StructuredDocumentTag sdt = new StructuredDocumentTag(doc, SdtType.PLAIN_TEXT, MarkupLevel.BLOCK);
sdt.getXmlMapping().setMapping(xmlPart, "/root[1]/text[1]", "");
doc.getFirstSection().getBody().appendChild(sdt);
doc.save(getArtifactsDir() + "SDT.CustomXml.docx");getId/setId | |
public java.lang.String getId() / public void setId(java.lang.String value) | |
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 structured document tag with a custom XML data.
Document doc = new Document();
// Construct an XML part that contains data and add it to the document's collection
// Once the "Developer" tab in Mircosoft Word is enabled,
// we can find elements from this collection as well as a couple defaults in the "XML Mapping Pane"
String xmlPartId = UUID.randomUUID().toString();
String xmlPartContent = "<root><text>Hello, World!</text></root>";
CustomXmlPart xmlPart = doc.getCustomXmlParts().add(xmlPartId, xmlPartContent);
// The data we entered resides in these variables
Assert.assertEquals(xmlPart.getData(), xmlPartContent.getBytes());
Assert.assertEquals(xmlPart.getId(), xmlPartId);
// XML parts can be referenced by collection index or GUID
Assert.assertEquals(doc.getCustomXmlParts().get(0), xmlPart);
Assert.assertEquals(doc.getCustomXmlParts().getById(xmlPartId), xmlPart);
// Once the part is created, we can add XML schema associations like this
xmlPart.getSchemas().add("http://www.w3.org/2001/XMLSchema");
// We can also clone parts and insert them into the collection directly
CustomXmlPart xmlPartClone = xmlPart.deepClone();
xmlPartClone.setId(UUID.randomUUID().toString());
doc.getCustomXmlParts().add(xmlPartClone);
Assert.assertEquals(doc.getCustomXmlParts().getCount(), 2);
// Iterate through collection with an enumerator 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++;
}
// XML parts can be removed by index
doc.getCustomXmlParts().removeAt(1);
Assert.assertEquals(doc.getCustomXmlParts().getCount(), 1);
// The XML part collection itself can be cloned also
CustomXmlPartCollection customXmlParts = doc.getCustomXmlParts().deepClone();
// And all elements can be cleared like this
customXmlParts.clear();
// Create a StructuredDocumentTag that will display the contents of our part,
// insert it into the document and save the document
StructuredDocumentTag sdt = new StructuredDocumentTag(doc, SdtType.PLAIN_TEXT, MarkupLevel.BLOCK);
sdt.getXmlMapping().setMapping(xmlPart, "/root[1]/text[1]", "");
doc.getFirstSection().getBody().appendChild(sdt);
doc.save(getArtifactsDir() + "SDT.CustomXml.docx");getSchemas | |
public CustomXmlSchemaCollection getSchemas() | |
Example:
Shows how to create structured document tag with a custom XML data.
Document doc = new Document();
// Construct an XML part that contains data and add it to the document's collection
// Once the "Developer" tab in Mircosoft Word is enabled,
// we can find elements from this collection as well as a couple defaults in the "XML Mapping Pane"
String xmlPartId = UUID.randomUUID().toString();
String xmlPartContent = "<root><text>Hello, World!</text></root>";
CustomXmlPart xmlPart = doc.getCustomXmlParts().add(xmlPartId, xmlPartContent);
// The data we entered resides in these variables
Assert.assertEquals(xmlPart.getData(), xmlPartContent.getBytes());
Assert.assertEquals(xmlPart.getId(), xmlPartId);
// XML parts can be referenced by collection index or GUID
Assert.assertEquals(doc.getCustomXmlParts().get(0), xmlPart);
Assert.assertEquals(doc.getCustomXmlParts().getById(xmlPartId), xmlPart);
// Once the part is created, we can add XML schema associations like this
xmlPart.getSchemas().add("http://www.w3.org/2001/XMLSchema");
// We can also clone parts and insert them into the collection directly
CustomXmlPart xmlPartClone = xmlPart.deepClone();
xmlPartClone.setId(UUID.randomUUID().toString());
doc.getCustomXmlParts().add(xmlPartClone);
Assert.assertEquals(doc.getCustomXmlParts().getCount(), 2);
// Iterate through collection with an enumerator 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++;
}
// XML parts can be removed by index
doc.getCustomXmlParts().removeAt(1);
Assert.assertEquals(doc.getCustomXmlParts().getCount(), 1);
// The XML part collection itself can be cloned also
CustomXmlPartCollection customXmlParts = doc.getCustomXmlParts().deepClone();
// And all elements can be cleared like this
customXmlParts.clear();
// Create a StructuredDocumentTag that will display the contents of our part,
// insert it into the document and save the document
StructuredDocumentTag sdt = new StructuredDocumentTag(doc, SdtType.PLAIN_TEXT, MarkupLevel.BLOCK);
sdt.getXmlMapping().setMapping(xmlPart, "/root[1]/text[1]", "");
doc.getFirstSection().getBody().appendChild(sdt);
doc.save(getArtifactsDir() + "SDT.CustomXml.docx");| Method Detail |
|---|
deepClone | |
public CustomXmlPart deepClone() | |
Example:
Shows how to create structured document tag with a custom XML data.
Document doc = new Document();
// Construct an XML part that contains data and add it to the document's collection
// Once the "Developer" tab in Mircosoft Word is enabled,
// we can find elements from this collection as well as a couple defaults in the "XML Mapping Pane"
String xmlPartId = UUID.randomUUID().toString();
String xmlPartContent = "<root><text>Hello, World!</text></root>";
CustomXmlPart xmlPart = doc.getCustomXmlParts().add(xmlPartId, xmlPartContent);
// The data we entered resides in these variables
Assert.assertEquals(xmlPart.getData(), xmlPartContent.getBytes());
Assert.assertEquals(xmlPart.getId(), xmlPartId);
// XML parts can be referenced by collection index or GUID
Assert.assertEquals(doc.getCustomXmlParts().get(0), xmlPart);
Assert.assertEquals(doc.getCustomXmlParts().getById(xmlPartId), xmlPart);
// Once the part is created, we can add XML schema associations like this
xmlPart.getSchemas().add("http://www.w3.org/2001/XMLSchema");
// We can also clone parts and insert them into the collection directly
CustomXmlPart xmlPartClone = xmlPart.deepClone();
xmlPartClone.setId(UUID.randomUUID().toString());
doc.getCustomXmlParts().add(xmlPartClone);
Assert.assertEquals(doc.getCustomXmlParts().getCount(), 2);
// Iterate through collection with an enumerator 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++;
}
// XML parts can be removed by index
doc.getCustomXmlParts().removeAt(1);
Assert.assertEquals(doc.getCustomXmlParts().getCount(), 1);
// The XML part collection itself can be cloned also
CustomXmlPartCollection customXmlParts = doc.getCustomXmlParts().deepClone();
// And all elements can be cleared like this
customXmlParts.clear();
// Create a StructuredDocumentTag that will display the contents of our part,
// insert it into the document and save the document
StructuredDocumentTag sdt = new StructuredDocumentTag(doc, SdtType.PLAIN_TEXT, MarkupLevel.BLOCK);
sdt.getXmlMapping().setMapping(xmlPart, "/root[1]/text[1]", "");
doc.getFirstSection().getBody().appendChild(sdt);
doc.save(getArtifactsDir() + "SDT.CustomXml.docx");