RE: [xml] Best Process for InnerXML from Xpath Query



Hello again:)

I must have caught a nerve or two judging by the response...not really sure
why...

Maybe I should try rewording the question...

I would like to be able to read the innerxml as a string result of an Xpath
query.

So, for example, if I have some xml such as:

...
<topnode>
<anode>
<bnode>sjdlkdsf</bnode>
<cnode>lkjlkdsf</cnode>
</anode>
<tnode>
<unode>sjsddlkdsf</unode>
<vnode>lkjlkfddsf</vnode>
</tnode>
</topnode>
...

and queried //anode the resulting string would be...

<bnode>sjdlkdsf</bnode>
<cnode>lkjlkdsf</cnode>


I wrote a C# wrapper which calls libxml2 to do this, but I am not entirely
happy with my approach as was looking for guidance as to a better way to do
it...

My example function is below:

[STAThread]
public static unsafe string singlenodeinnerxml(bool SourceIsString,string
sourcexml, string XPathExpression, System.Text.Encoding xmlencoding)
{
        xmlInitParser();
        _xmlDoc *doc;
        if (SourceIsString == true)
        {
                doc = xmlParseMemory(new
System.Text.StringBuilder(sourcexml),sourcexml.Length);
        }
        else
        {
                doc = xmlParseFile(sourcexml);
        }
        if (doc == null)
        {
                return "";
        }
        _xmlXPathContext *xpathCtx = xmlXPathNewContext(doc);
        if(xpathCtx == null) 
        {
                xmlFreeDoc(doc);
                return "";
        }
        _xmlXPathObject *xpathObj = xmlXPathEval(XPathExpression ,xpathCtx);
        if(xpathObj == null || xpathObj->nodesetval == null)
        {
                xmlXPathFreeContext(xpathCtx);
                xmlFreeDoc(doc);
                return "";
        }
        Console.WriteLine("\n[" + xpathObj->type1.ToString() + "]\n");
        _xmlBuffer *mybuffer = xmlBufferCreate();
        Console.WriteLine("\n[" + xmlNodeDump(mybuffer, doc,
xpathObj->nodesetval->nodeTab[0], 0, 0).ToString() + "] bytes written\n");
        byte *mybyte = xmlBufferContent(mybuffer);
        int xpathlen = xmlStrlen(mybyte);
        byte[] bytesArray = new byte[xpathlen];
        for (int i = 0; i < xpathlen; i ++)
        {
                bytesArray[i] = *(mybyte + i);
        }
        xmlBufferFree(mybuffer);
        xmlXPathFreeObject(xpathObj);
        xmlXPathFreeContext(xpathCtx);
        xmlFreeDoc(doc);
        string mystring = xmlencoding.GetString(bytesArray);
        // mystring contains .outerXml
        // the below string manipulations convert .outerXml into .innerXml,
but the design is poor...
        mystring = mystring.Substring(0,mystring.LastIndexOf("<")); //
remove closing outerelement
        mystring = mystring.Substring(mystring.IndexOf(">") + 1); // remove
starting outerelement
        System.GC.Collect();
        System.Windows.Forms.Application.DoEvents();
        return mystring;
}


Best Regards,

Alex


-----Original Message-----
From: Daniel Veillard [mailto:veillard redhat com] 
Sent: Wednesday, July 13, 2005 7:25 AM
To: Alex Neblett
Cc: xml gnome org
Subject: Re: [xml] Best Process for InnerXML from Xpath Query

On Wed, Jul 13, 2005 at 07:21:25AM -0500, Alex Neblett wrote:
Hello,

What is the best process to get the innerxml from an Xpath query using 
the latest libxml2?

  Do NOT use Reply on an existing mail thread to ask a completely unrelated
question :-(
  And I don't understand your question ... at all !

Daniel

-- 
Daniel Veillard      | Red Hat Desktop team http://redhat.com/
veillard redhat com  | libxml GNOME XML XSLT toolkit  http://xmlsoft.org/
http://veillard.com/ | Rpmfind RPM search engine http://rpmfind.net/




[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]