Page 1 of 1

Use XML, XPATH to extract info from content.xml

Posted: Sun Jul 16, 2023 2:22 pm
by ms777
Hi,

bitmaps stored in OO document can be extracted by unzipping the document. Their path is an internally given name.
To find out which image belongs to which item in the document the below code snippet lists all images with their name and the path in the zip

Good luck,

ms777

Code: Select all

Sub ListStoredImages
	oDoc = ThisComponent
	
'create an XML document from the content.xml
	oDomBuilder = createUnoService("com.sun.star.xml.dom.DocumentBuilder")
	oDom = oDomBuilder.parse(oDoc.DocumentStorage.getByName("content.xml").Inputstream)
	
'create an XPATH to search for image nodes, which are direct childs of a frame node
	oXPath = createUnoService("com.sun.star.xml.xpath.XPathAPI")
	oNL = oXPath.selectNodeList(oDom, "//*[name()='draw:frame']/*[name()='draw:image']" )
	
	for k=0 to oNL.length-1
		oItem = oNL.item(k)
		sHref = oItem.getAttribute("href")
		sName = oItem.ParentNode.getAttribute("name")
		print sName + ": " + sHref
		
'if you want to extract the image data ...
'		oPic = oDoc.DocumentStorage.getByName("Pictures").getByName(Replace(sHref, "Pictures/", ""))
'		oPic has an InputStream, where you can read the data from
'	    xray oPic

	next k

End Sub