Page 1 of 1

getJAXBNodesViaXPath

PostPosted: Fri Jul 22, 2011 1:57 am
by Tarfaa
Hi all,

I am on a project and I need to get all the text from a PowerPoint presentation.
I have read the documentation about it and I would get back txBody or paragraph.

I tried to use a home made traversal (that uses TraversalUtil.getChildrenImpl()) but it does not find any CTTextParagraph.
Thus I tried to use it with Shape and that's working. But not all CTTextParagraph are in such Shape...

Finaly I tried to do it with getJAXBNodesViaXPath. It's easy with docx, but with pptx I am a little lost.

Which binder must I use ?
Code: Select all
pres.getMainPresentationPart().getJAXBContext().createBinder()


And which xPath ?
Code: Select all
"//a:p"
or
Code: Select all
"//p:txBody"


And on which objects ?
Code: Select all
((SlidePart) part).getJaxbElement().getCSld().getSpTree().getSpOrGrpSpOrGraphicFrame()


If you have any idea how I could resolve these issues...

thx

Re: getJAXBNodesViaXPath

PostPosted: Fri Jul 22, 2011 11:18 pm
by jason
You need to traverse or use XPath on individual slides (SlidePart), rather than the MainPresentationPart.

See org.pptx4j.samples.TraverseSlide. The current version in svn shows how to read p:txBody.

Re: getJAXBNodesViaXPath

PostPosted: Wed May 23, 2012 5:51 pm
by arahant7
Is there a getJAXBNodesViaXPath method in pptx4j somewhere. I can't seem to find it.
If not, then which method I should use (if exists) for XPath?

Re: getJAXBNodesViaXPath

PostPosted: Wed May 23, 2012 6:42 pm
by jason
Have a look at SlidePart.

Re: getJAXBNodesViaXPath

PostPosted: Wed May 23, 2012 7:28 pm
by arahant7
Yes I did. There's no getJAXBNodesViaXPath method. There's a getJAXBContext and getJAXBElement only.

I also found a method called getJAXBNodesViaXPath in XmlUtils, but that keeps throwing xpath expression exception (even for valid xpaths)
Here's how I use it
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
SlidePart slide = (SlidePart)presentationMLPackage.getParts().get(new PartName("/ppt/slides/slide4.xml") );
XmlUtils.getJAXBNodesViaXPath(slide.getJAXBContext().createBinder(), slide, "/", false);
 
Parsed in 0.015 seconds, using GeSHi 1.0.8.4


Exception in thread "main" java.lang.RuntimeException: javax.xml.xpath.XPathExpressionException
at org.docx4j.XmlUtils.xpath(XmlUtils.java:915)
at org.docx4j.XmlUtils.xpath(XmlUtils.java:888)
at org.docx4j.XmlUtils.getJAXBNodesViaXPath(XmlUtils.java:866)
at com.topcoder.pptx.SimpleSample.main(SimpleSample.java:185)
Caused by: javax.xml.xpath.XPathExpressionException
at org.apache.xpath.jaxp.XPathImpl.evaluate(XPathImpl.java:295)
at org.docx4j.XmlUtils.xpath(XmlUtils.java:907)
... 3 more
Caused by: javax.xml.transform.TransformerException: Unknown error in XPath.
at org.apache.xpath.XPath.execute(XPath.java:365)
at org.apache.xpath.jaxp.XPathImpl.eval(XPathImpl.java:219)
at org.apache.xpath.jaxp.XPathImpl.evaluate(XPathImpl.java:281)
... 4 more
Caused by: java.lang.NullPointerException
at org.apache.xpath.axes.ChildTestIterator.setRoot(ChildTestIterator.java:127)
at org.apache.xpath.axes.OneStepIterator.setRoot(OneStepIterator.java:95)
at org.apache.xpath.axes.NodeSequence.setRoot(NodeSequence.java:265)
at org.apache.xpath.axes.LocPathIterator.execute(LocPathIterator.java:212)
at org.apache.xpath.XPath.execute(XPath.java:337)
... 6 more

Re: getJAXBNodesViaXPath

PostPosted: Wed May 23, 2012 9:44 pm
by jason
There is now, but possibly wasn't in 2.7.1. Try the 2.8.0 SNAPSHOT.

slide.getJAXBContext().createBinder() without more is an incorrect usage.

Re: getJAXBNodesViaXPath

PostPosted: Wed May 23, 2012 10:17 pm
by arahant7
slide.getJAXBContext().createBinder() without more is an incorrect usage.

Can you comment what is the correct usage? I cannot figure it out by looking at the method implementation.
Thanks

Re: getJAXBNodesViaXPath

PostPosted: Wed May 23, 2012 11:04 pm
by jason