LibXML2 Static XPath Test Crossplatform

Applications, Games, Tools, User libs and useful stuff coded in PureBasic
Justin
Addict
Addict
Posts: 948
Joined: Sat Apr 26, 2003 2:49 pm

LibXML2 Static XPath Test Crossplatform

Post by Justin »

I compiled libxml2 statically for Windows to test the XPath support, it may be useful if someone needs xpath.
I disabled libiconv wich was LGPL so now everything should be MIT.

The project is at github, simply run xpathTest.pb, it's a very simple xpath test, just a proof of concept:
https://github.com/omegakode/PBLibXML2

LibXML2 is crossplatform Win64, Linux and MacOS supported.

Linux:
sudo apt-get install libxml2-dev

MacOS:
Install MacPorts
https://www.macports.org/
And:
sudo port install libxml2

LibXML2 source if you want to compile it by yourself:
https://github.com/GNOME/libxml2

LibXML2 manual:
https://gnome.pages.gitlab.gnome.org/li ... index.html

XPath 1.0
https://www.w3.org/TR/1999/REC-xpath-19991116/

The xpathTest.pb example:

Code: Select all

;xpathTest.pb

EnableExplicit

IncludeFile "libxml2.pbi"

Procedure main()
	Protected.xmlXPathContext *xpCtx
	Protected.xmlDoc *doc
	Protected.s xml, xp
	Protected.xmlXPathObject *xpObj
	Protected.l i, sz
	Protected.xmlNodeSet *nodeSet
	Protected.xmlNode *node
	Protected.i attVal
	Protected.xmlAttr *attr
	
	xmlInitParser()
	
	xml = "test3.xml"
	*doc = xmlParseFile(xml)
	If *doc = 0 : ProcedureReturn : EndIf 
	
	*xpCtx = xmlXPathNewContext(*doc)
	If *xpCtx = 0 : ProcedureReturn : EndIf 

	xp = "//child2"
	*xpObj = xmlXPathEvalExpression(xp, *xpCtx)
	If *xpObj = 0 : ProcedureReturn : EndIf 

	*nodeSet = *xpObj\nodesetval
	If *nodeSet = 0 : ProcedureReturn : EndIf 

	sz = *nodeSet\nodeNr	
	For i = 0 To sz - 1
		*node = *nodeSet\nodeTab\node[i]
		
		If *node\type = #XML_ELEMENT_NODE
			If *node\name
				Debug PeekS(*node\name, -1, #PB_UTF8)
			EndIf
			
			;Attributes
			*attr = *node\properties
			While *attr
				Debug "Attr:"
				If *attr\name
					Debug PeekS(*attr\name, -1, #PB_UTF8)
				EndIf
				
				attVal = xmlNodeListGetString(*node\doc, *attr\children, 1)
				If attVal
					Debug PeekS(attVal, -1, #PB_UTF8)
					xmlMemFree(attVal)
				EndIf
				
				*attr = *attr\next
			Wend 
		Else
		
		EndIf
	Next 
	
	xmlXPathFreeObject(*xpObj);
	xmlXPathFreeContext(*xpCtx); 
	xmlFreeDoc(*doc)
EndProcedure

main()
Last edited by Justin on Fri Nov 01, 2024 3:59 pm, edited 3 times in total.
User avatar
idle
Always Here
Always Here
Posts: 5896
Joined: Fri Sep 21, 2007 5:52 am
Location: New Zealand

Re: LibXML2 Static XPath test

Post by idle »

That looks really useful.
Justin
Addict
Addict
Posts: 948
Joined: Sat Apr 26, 2003 2:49 pm

Re: LibXML2 Static XPath test

Post by Justin »

Updated for Linux, if you get some error use:
sudo apt-get install libxml2-dev
Justin
Addict
Addict
Posts: 948
Joined: Sat Apr 26, 2003 2:49 pm

Re: LibXML2 Static XPath Test Crossplatform

Post by Justin »

Updated for MacOS, now is crossplatform.

Install MacPorts
https://www.macports.org/
And:
sudo port install libxml2
Post Reply