The bib custom xml part is a rel of the Main Document Part (as shown by running it through the webapp or PartsList):
- Code: Select all
Part /word/document.xml [org.docx4j.openpackaging.parts.WordprocessingML.MainDocumentPart] http://schemas.openxmlformats.org/officeDocument/2006/relationships/officeDocument containing JaxbElement:org.docx4j.wml.Document
content type: application/vnd.openxmlformats-officedocument.wordprocessingml.document.main+xml
Part /customXml/item1.xml [org.docx4j.openpackaging.parts.WordprocessingML.BibliographyPart] http://schemas.openxmlformats.org/officeDocument/2006/relationships/customXml containing JaxbElement:{http://schemas.openxmlformats.org/officeDocument/2006/bibliography}Sources is a javax.xml.bind.JAXBElement; it has declared type org.docx4j.bibliography.CTSources
content type: application/xml
Part /customXml/itemProps1.xml [org.docx4j.openpackaging.parts.CustomXmlDataStoragePropertiesPart] http://schemas.openxmlformats.org/officeDocument/2006/relationships/customXmlProps containing JaxbElement:org.docx4j.customXmlProperties.DatastoreItem
content type: application/vnd.openxmlformats-officedocument.customXmlProperties+xml
As for any other part, you delete the rel.
Using java Syntax Highlighting
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import org.docx4j.openpackaging.exceptions.Docx4JException;
import org.docx4j.openpackaging.packages.WordprocessingMLPackage;
import org.docx4j.openpackaging.parts.CustomXmlDataStoragePropertiesPart;
import org.docx4j.openpackaging.parts.Part;
import org.docx4j.openpackaging.parts.WordprocessingML.BibliographyPart;
import org.docx4j.openpackaging.parts.relationships.Namespaces;
import org.docx4j.openpackaging.parts.relationships.RelationshipsPart;
import org.docx4j.relationships.Relationship;
public class DeleteBibCXP
{
static String file
= System.
getProperty("user.dir") + "/Bibliography.docx";
public static void main
(String[] args
) throws Exception {
WordprocessingMLPackage
wordMLPackage = WordprocessingMLPackage.
load(new File(file
));
RelationshipsPart rp
= wordMLPackage.
getMainDocumentPart().
getRelationshipsPart();
List
<Relationship
> deletions
= new ArrayList
<Relationship
>();
for ( Relationship r
: rp.
getRelationships().
getRelationship() ) {
System.
out.
println("For Relationship Id=" + r.
getId()
+ " Source is " + rp.
getSourceP().
getPartName()
+ ", Target is " + r.
getTarget() );
if (r.
getTargetMode() != null
&& r.
getTargetMode().
equals("External") ) {
continue;
}
try {
Part part
= rp.
getPart(r
);
if (part
instanceof BibliographyPart
) {
deletions.
add(r
);
// it is also stored by itemId in a hashmap, so for completeness, delete it from there
String itemId
= getItemId
((BibliographyPart
)part
);
if (itemId
!=null) {
System.
out.
println("deleting " + itemId
);
wordMLPackage.
getCustomXmlDataStorageParts().
remove(itemId
);
}
}
} catch (Exception e
) {
throw new Docx4JException
("Failed to add parts from relationships", e
);
}
}
for ( Relationship r
: deletions
) {
System.
out.
println("deleting " + r.
getId() );
rp.
removeRelationship(r
);
}
}
public static String getItemId
(BibliographyPart entry
) {
String itemId
= null;
if (entry.
getRelationshipsPart()==null) {
return null;
} else {
// Look in its rels for rel of @Type customXmlProps (eg @Target="itemProps1.xml")
Relationship r
= entry.
getRelationshipsPart().
getRelationshipByType(
Namespaces.
CUSTOM_XML_DATA_STORAGE_PROPERTIES);
if (r
==null) {
System.
out.
println(".. but that doesn't point to a customXmlProps part");
return null;
}
CustomXmlDataStoragePropertiesPart customXmlProps
=
(CustomXmlDataStoragePropertiesPart
)entry.
getRelationshipsPart().
getPart(r
);
if (customXmlProps
==null) {
System.
out.
println(".. but the target seems to be missing?");
return null;
} else {
return customXmlProps.
getItemId().
toLowerCase();
}
}
}
}
Parsed in 0.021 seconds, using
GeSHi 1.0.8.4
A Bibliography part, being a CustomXML part, is also "registered" by item id during loading. So the above code also deletes it from that.
- Code: Select all
INFO org.docx4j.openpackaging.io.Load .registerCustomXmlDataStorageParts line 301 - Found a CustomXmlPart, named /customXml/item1.xml
DEBUG org.docx4j.openpackaging.io.Load .registerCustomXmlDataStorageParts line 306 - .. it has a rels part
INFO org.docx4j.openpackaging.parts.JaxbXmlPart .getJaxbElement line 129 - Lazily unmarshalling /customXml/itemProps1.xml
INFO org.docx4j.openpackaging.io.Load .registerCustomXmlDataStorageParts line 343 - Identified/registered ds:itemId {bececd4f-4e83-45f8-a8f4-bd5e03b7a7e5}