Re: [xml] Get sub-document from xmlDocPtr
- From: Andrew Hartley <andrew hartley bcs org uk>
- To: xml gnome org
- Subject: Re: [xml] Get sub-document from xmlDocPtr
- Date: Thu, 17 Jul 2008 06:13:53 -0700 (PDT)
Daniel Veillard wrote:
On Thu, Jul 17, 2008 at 03:25:20AM -0700, Andrew Hartley wrote:
Andrew Hartley wrote:
Having extracted a sub-document with an XPath expression and now have
an
xmlXPathObjectPtr is there a simple way to output the sub-document into
a
new xmlDocPtr so that this sub-document can then be saved to disk?
It's ok, I have worked it out:
[code]
xmlDocPtr pXmlSubDoc = pXPathObj->nodesetval->nodeTab[0]->doc;
[/code]
Note that an XPath object could also be a number, a boolean or a string,
so don't blindly do that dereference and check pXPathObj type first, and
then that the pointer is not null, and then that the node set at least has
one node, and that this node is not a namespace (namespace nodes don't
have
pointer back to the document).
Basically there is tons of checking missing there, and i would prefer
if other people didn't just cut and past that everywhere...
I think it really makes more sense to keep the document pointer around,
for example it's embedded in the XPath evaluation context. Beware of bad
scoping/API design that happen frequently when you don't have yet a good
vison of the objects manipulated.
Daniel
--
Red Hat Virtualization group http://redhat.com/virtualization/
Daniel Veillard | virtualization library http://libvirt.org/
veillard redhat com | libxml GNOME XML XSLT toolkit http://xmlsoft.org/
http://veillard.com/ | Rpmfind RPM search engine http://rpmfind.net/
_______________________________________________
xml mailing list, project page http://xmlsoft.org/
xml gnome org
http://mail.gnome.org/mailman/listinfo/xml
Thanks for the reply Daniel. However, I only wanted to explain that I had
solved the problem. Here is a little more of my code just in case others
have the same problem:
[code]
xmlDocPtr pXmlDoc = xmlParseFile( szFilepath );
if ( pXmlDoc )
{
xmlXPathContextPtr pXPathCtx = xmlXPathNewContext( pXmlDoc );
CString sNsUri = rDoc.GetNameSpaceURI();
// Register namespace we are just abut to use:
xmlXPathRegisterNs( pXPathCtx, (const xmlChar*)"ct1", (const
xmlChar*)(LPCTSTR)sNsUri );
// XPath expression to get the CT1 nodeset:
xmlXPathObjectPtr pXPathObj = xmlXPathEvalExpression( (const
xmlChar*)"//ct1:Ct1", pXPathCtx );
if ( pXPathObj )
{
xmlNodeSetPtr pNodeSet = pXPathObj->nodesetval;
if ( pNodeSet && pNodeSet->nodeNr > 0
&& pNodeSet->nodeTab[0]->type != XML_NAMESPACE_DECL
&& pNodeSet->nodeTab[0] )
{
// sub-document containing only the CT1 nodeset:
xmlDocPtr pXmlSubDoc = pNodeSet->nodeTab[0]->doc;
// ... and save:
xmlSaveCtxtPtr pSaveCtx = xmlSaveToFilename(
sFilenameCopyTo.c_str(), NULL, 0 );
long ret = xmlSaveDoc( pSaveCtx, pXmlSubDoc );
ret = xmlSaveClose( pSaveCtx );
++nCopied;
xmlXPathFreeObject( pXPathObj );
}
}
xmlXPathFreeContext( pXPathCtx );
xmlFreeDoc( pXmlDoc );
}
[/code]
--
View this message in context: http://www.nabble.com/Get-sub-document-from-xmlDocPtr-tp18504765p18507651.html
Sent from the Gnome - Lib - Xml - General mailing list archive at Nabble.com.
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]