You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@poi.apache.org by Chris Bamford <cb...@mimecast.com> on 2015/01/09 12:35:25 UTC

Navigating relationships in OOXML docs

Hi there

I need to look for a relationship in the ".rels" file of the format-specific document, not the main package itself.
By this I mean not in the "_rels/.rels" file in the top level but in (say) the "xl/_rels/workbook.xml.rels" file.
The OPCPackage class looks promising, but so far I have only been able to make it work with the former file.

Is there an elegant way to do this, or must I do something like:


OPCPackage pkg = OPCPackage.open(filename);

PackageRelationship relationship = pkg.getRelationshipsByType(PackageRelationshipTypes.CORE_DOCUMENT).getRelationship(0);
PackagePart part = pkg.getPart(relationship);
String mainEntryName = part.getPartName().getName();
String[] parts = mainEntryName.split("/");
String docRels = "/" + parts[1] + "/_rels/" + parts[2] + ".rels";  // e.g. "/xl/_rels/workbook.xml.rels"
Pattern p = Pattern.compile(docRels);

PackagePart p2 = pkg.getPartsByName(p).get(0);

...

This seems clumsy!  It also has to work for all 3 OOXML types (pptx, dock, xlxx).

Thanks,

- Chris

Chris Bamford
Senior Developer
m: +44 7860 405292
p: +44 207 847 8700
w: www.mimecast.com
Address click here: www.mimecast.com/About-us/Contact-us/







Re: Navigating relationships in OOXML docs

Posted by Chris Bamford <cb...@mimecast.com>.
Thanks Nick (and Dominik) for your suggestions.

- Chris

Chris Bamford
Senior Developer
m: +44 7860 405292
p: +44 207 847 8700
w: www.mimecast.com
Address click here: www.mimecast.com/About-us/Contact-us/

On 11 Jan 2015, at 23:25, Nick Burch <ap...@gagravarr.org> wrote:

> On Fri, 9 Jan 2015, Chris Bamford wrote:
>> I need to look for a relationship in the ".rels" file of the format-specific document, not the main package itself. By this I mean not in the "_rels/.rels" file in the top level but in (say) the "xl/_rels/workbook.xml.rels" file.
> 
> There's already POI code to do that, but it's a little fiddly. Try looking at something like getNotes() on XSLFSlide, and the code it calls underneath, to see how to navigate the relations linking from a given part
> 
> Nick
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@poi.apache.org
> For additional commands, e-mail: user-help@poi.apache.org
> 




Re: Navigating relationships in OOXML docs

Posted by Nick Burch <ap...@gagravarr.org>.
On Fri, 9 Jan 2015, Chris Bamford wrote:
> I need to look for a relationship in the ".rels" file of the 
> format-specific document, not the main package itself. By this I mean 
> not in the "_rels/.rels" file in the top level but in (say) the 
> "xl/_rels/workbook.xml.rels" file.

There's already POI code to do that, but it's a little fiddly. Try looking 
at something like getNotes() on XSLFSlide, and the code it calls 
underneath, to see how to navigate the relations linking from a given part

Nick

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@poi.apache.org
For additional commands, e-mail: user-help@poi.apache.org


Re: Navigating relationships in OOXML docs

Posted by Dominik Stadler <do...@gmx.at>.
What about the following:

OPCPackage pkg = OPCPackage.open(filename);

pkg.getPart(PackagingURIHelper.createPartName("/xl/_rels/workbook.xml.rels"));


Dominik.

On Fri, Jan 9, 2015 at 12:35 PM, Chris Bamford <cb...@mimecast.com>
wrote:

>
>  Hi there
>
>  I need to look for a relationship in the ".rels" file of the
> format-specific document, not the main package itself.
> By this I mean not in the "_rels/.rels" file in the top level but in
> (say) the "xl/_rels/workbook.xml.rels" file.
> The OPCPackage class looks promising, but so far I have only been able to
> make it work with the former file.
>
>  Is there an elegant way to do this, or must I do something like:
>
>  OPCPackage pkg = OPCPackage.open(filename);
>
> PackageRelationship relationship = pkg.getRelationshipsByType(PackageRelationshipTypes.CORE_DOCUMENT).getRelationship(0);
> PackagePart part = pkg.getPart(relationship);
> String mainEntryName = part.getPartName().getName();
> String[] parts = mainEntryName.split("/");
> String docRels = "/" + parts[1] + "/_rels/" + parts[2] + ".rels";  // e.g. "/xl/_rels/workbook.xml.rels"
> Pattern p = Pattern.compile(docRels);
>
> PackagePart p2 = pkg.getPartsByName(p).get(0);
>
> ...
>
>
>  This seems clumsy!  It also has to work for all 3 OOXML types (pptx,
> dock, xlxx).
>
>  Thanks,
>
>  - Chris
>
>
>   Chris Bamford m: +44 7860 405292  w: www.mimecast.com  Senior Developer p:
> +44 207 847 8700 Address click here
> <http://www.mimecast.com/About-us/Contact-us/>
> ------------------------------
>  [image: http://www.mimecast.com]
> <https://serviceA.mimecast.com/mimecast/click?account=C1A1&code=3d0286661f7ded1d012a6a763f68f5d7>
>   [image: LinkedIn]
> <https://serviceA.mimecast.com/mimecast/click?account=C1A1&code=2e0d0852331c58c0317ea89a9cc669ae> [image:
> YouTube]
> <https://serviceA.mimecast.com/mimecast/click?account=C1A1&code=704a83df5e431323df35710fa8b4bee8> [image:
> Facebook]
> <https://serviceA.mimecast.com/mimecast/click?account=C1A1&code=045fddd22dbf0ac78019fa77dd927a2c> [image:
> Blog]
> <https://serviceA.mimecast.com/mimecast/click?account=C1A1&code=177451b2d5c813751e36ce89faf43035> [image:
> Twitter]
> <https://serviceA.mimecast.com/mimecast/click?account=C1A1&code=1f5be7d1e6edde6db20e9b0ec91a6186>
>
>