AutoHotkey Homepage AutoHotkey Community
Let's help each other out
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Microsoft.XMLDOM

 
Reply to topic    AutoHotkey Community Forum Index -> Scripts & Functions
View previous topic :: View next topic  
Author Message
majkinetor



Joined: 24 May 2006
Posts: 4511
Location: Belgrade

PostPosted: Fri Jan 04, 2008 9:27 pm    Post subject: Microsoft.XMLDOM Reply with quote

Some tests with MS DOM parser.

Current API:
Code:
XML_IsTextNode(pNode)
XML_Node(pNodes, idx)
XML_Open( pDoc, pAsync=false, pShowErr=true )
XML_childNodes(pNode)
XML_firstChild(pNode)
XML_hasChildNodes(pNode)
XML_lastChild(pNode)
XML_length(pNodes)
XML_nextSibling(pNode)
XML_nodeName(pNode)
XML_nodeType(pNode)
XML_parentNode(pNode)
XML_previousSibling(pNode)
XML_selectNodes( pNode, xPath )
XML_selectSingleNode( pNode, xPath )
XML_NodeText(pNode, xpath)



Test and functions:
Code:
;XML test
;   by majkinetor
;   docs: http://msdn2.microsoft.com/en-us/library/aa468547.aspx

xDoc := XML_Open("http://cyber.law.harvard.edu/rss/examples/rss2sample.xml")
if !xDoc
   return

xpath := "//item"
msgbox % DisplayNode( XML_childNodes(xDoc) )

nodes :=  XML_selectNodes(xDoc, xpath )

msgbox % "XPath: " xpath "`n`n" DisplayNode(nodes)

return

DisplayNode(xcn, ident="") {
   loop, % XML_length(xcn)
   {
      xNode := XML_Node(xcn, A_Index)
      if txt := XML_IsTextNode(xNode)
         res .=   ident XML_nodeName(xNode) ": " txt "`n"
      else if XML_hasChildNodes(xNode)
      {
         res .= ident XML_nodeName(xNode) "`n"
         res .= DisplayNode(  XML_childNodes(xNode), ident "   ")
      }
   }

   return res
}
;------------------------------------------------------------------------------

XML_Open( pDoc, pAsync=false, pShowErr=true ){
   COM_CoInitialize()
   xDoc := COM_ActiveXObject("Msxml2.DOMDocument.3.0")
   COM_Invoke(xDoc, "async=", pAsync)
   if COM_Invoke(xDoc, "Load", pDoc)
      return xDoc

   ;error
   if pShowErr
   {
      xPE := COM_Invoke(xDoc, "parseError")
      COM_Invoke(xPE, "errorCode")
      err := "XML Document failed to load`n"
          . "`nError:  "         COM_Invoke(xPE,"errorCode") 
         . "`n"               COM_Invoke(xPE,"reason")
           . "`nLine: "         COM_Invoke(xPe,"Line")
           . "`nLine Position: "   COM_Invoke(xPe,"linepos")
           . "`nFile Position: "   COM_Invoke(xPe,"filepos")
           . "`nSource Text: "      COM_Invoke(xPe,"srcText")
           . "`nDocument URL: "   COM_Invoke(xPe,"url")
   }
   
   return 0
}

XML_IsTextNode(pNode){
   fc := COM_Invoke(pNode, "firstChild")
   if COM_Invoke(fc, "nodeType" ) = 3 ;NODE_TEXT
      return COM_Invoke(fc, "nodeValue")
}

XML_selectNodes( pNode, xPath ) {
  return COM_Invoke(pNode, "selectNodes", xPath)
}

XML_selectSingleNode( pNode, xPath ) {
  return COM_Invoke(pNode, "selectSingleNode", xPath)
}

XML_nodeType(pNode) {
   static TYPE_1="ELEMENT", TYPE_2="ATTRIBUTE", TYPE_3="TEXT", TYPE_4="CDATA_SECTION", TYPE_5="ENTITY_REFERENCE", TYPE_6="ENTITY", TYPE_7="PROCESSING_INSTRUCTION", TYPE_8="COMMENT", TYPE_9="DOCUMENT", TYPE_10="DOCUMENT_TYPE", TYPE_11="DOCUMENT_FRAGMENT", TYPE_12="NOTATION"
   res := COM_Invoke(pNode,"nodeType")
   return TYPE_%res%
}

XML_nodeName(pNode) {
   return COM_Invoke(pNode, "nodeName")   
}

XML_parentNode(pNode) {
   return COM_Invoke(pNode, "parentNode")
}

XML_firstChild(pNode) {
   return COM_Invoke(pNode, "firstChild")
}

XML_lastChild(pNode) {
   return COM_Invoke(pNode, "lastChild")
}

XML_previousSibling(pNode){
   return COM_Invoke(pNode, "previousSibling")
}

XML_nextSibling(pNode){
   return COM_Invoke(pNode, "nextSibling")
}


XML_hasChildNodes(pNode) {
   return COM_Invoke(pNode, "hasChildNodes")
}

XML_childNodes(pNode) {
   return COM_Invoke(pNode, "childNodes")
}

XML_length(pNodes){
   return COM_Invoke(pNodes, "length")
}

XML_Node(pNodes, idx) {
   return COM_Invoke(pNodes, "Item", idx-1)
}

XML_NodeText(pNode, xpath) {
   return COM_Invoke(COM_Invoke(pNode, "selectSingleNode", xpath "/text()"), "nodeValue")
}


_________________


Last edited by majkinetor on Fri Jan 04, 2008 10:41 pm; edited 1 time in total
Back to top
View user's profile Send private message
COM TTS
Guest





PostPosted: Fri Jan 04, 2008 10:38 pm    Post subject: Reply with quote

MS DOM parser

Yeah, I always thought when we have COM module, why not use msxml!

Thanks, majkintor!

Keep it up!
Back to top
COM TTS
Guest





PostPosted: Fri Jan 04, 2008 10:43 pm    Post subject: Reply with quote

And here is another free xmlparser.
it's free and COM interface.

http://www.chilkatsoft.com/XML-ActiveX.asp


Even though I'm a just user, when I saw the vb example presented on the site, I could do somthing i wanted.


and another free componet

http://www.chilkatsoft.com/SpiderActiveX.asp
Back to top
derRaphael



Joined: 23 Nov 2007
Posts: 841
Location: ~/.

PostPosted: Fri Jan 04, 2008 11:14 pm    Post subject: Reply with quote

thank you majkinetor, this just comes in handy
_________________

    All scripts, unless otherwise noted, are hereby released under CC-BY
Back to top
View user's profile Send private message
pantagruel



Joined: 08 Oct 2006
Posts: 96
Location: denmark

PostPosted: Mon Oct 13, 2008 7:30 pm    Post subject: How do I append, cloneNodes Reply with quote

Hi, I guess I need to appendNode and CloneNode with MSXML, unfortunately I don't understand how the Com_Invoke works well enough to do it. Can you explain to me how to make appendNode work and then I can maybe get to work on the rest of methods.
Back to top
View user's profile Send private message
Lexikos



Joined: 17 Oct 2006
Posts: 7295
Location: Australia

PostPosted: Wed Oct 15, 2008 7:26 am    Post subject: Reply with quote

Given a pointer to a node to clone (pnc) and a pointer to a node to append it to (pnp), cloneNode and appendChild can be called as follows:
Code:
; Passing true clones descendants recursively:
pclone := COM_Invoke(pnc, "cloneNode", true)
; "+" indicates an object pointer:
COM_Invoke(pnp, "appendChild", "+" pclone)
; Free up unneeded references:
COM_Release(pclone)
(Untested)
Back to top
View user's profile Send private message Visit poster's website
pantagruel



Joined: 08 Oct 2006
Posts: 96
Location: denmark

PostPosted: Wed Oct 15, 2008 7:57 am    Post subject: thanks, so this means.. Reply with quote

thanks,

So I just want to make sure I understand what's happening in the (untested) code:

pclone - COM_Invoke takes a node (pnc), the name of the method to run on this node ("cloneNode"), and after the last , is a number of parameters - whatever number of parameters the actual method takes.

Thus with appendChild you have a node pnp, the method appendChild and the "+" is a pointer to another object, in this case pclone which we just defined. The "+" and pclone come after the second command so these are interpreted to build up the actual parameter being passed to appendChild

I'm hoping this is correct, because if it is COM_Invoke seems reasonably easy to work with.
Back to top
View user's profile Send private message
Lexikos



Joined: 17 Oct 2006
Posts: 7295
Location: Australia

PostPosted: Wed Oct 15, 2008 8:26 am    Post subject: Reply with quote

Your understanding seems sound. Smile
Back to top
View user's profile Send private message Visit poster's website
amokoura



Joined: 14 Nov 2006
Posts: 15

PostPosted: Tue Jan 06, 2009 7:57 am    Post subject: Reply with quote

It's amazing, thanks!

I tried to use xpath function: translate() but received error: "Unknown method".
According to this page the problem can be fixed by modifying XML_Open:
Code:


XML_Open( pDoc, pAsync=false, pShowErr=true ){
   [... snip ...]
   COM_Invoke(xDOC, "setProperty", "SelectionLanguage", "XPath")
   [... snip ...]
}


There was mentioned that the problem doesn't exist in MSXML4. Any reason we're using "Msxml2.DOMDocument.3.0"?

BTW my xpath was like: "/Root/Element/[translate(PersonName,'åäö','aao')='Jöhn Doe']/Age"
_________________
I recommend AutoIt instead of AHK.
Back to top
View user's profile Send private message
bmcclure



Joined: 24 Nov 2007
Posts: 774

PostPosted: Thu Feb 26, 2009 1:32 am    Post subject: Reply with quote

I'm making an XML class, and a supporting XMLNode class, which will be on object-oriented approach to working with XML documents through AHK (using MSXML)

It seems you've already done some of the groundwork that I would have otherwise done; would you mind if I based some of my functions off of what you've already created here?

Either way, thanks for showing me how to access MSXML Smile
_________________
Ben

My Trac projects
My Wiki
[Broken] - My music
Back to top
View user's profile Send private message
Guest






PostPosted: Thu Feb 26, 2009 7:54 am    Post subject: Reply with quote

awesome, thanks very much! Very Happy
Back to top
majkinetor !
Guest





PostPosted: Thu Feb 26, 2009 1:48 pm    Post subject: Reply with quote

As always, all my code is open source, and you can do with it whatever you want as long as you put appropriate credit.

There aren't many other functions to be added tho, AFAIK.
Back to top
bmcclure



Joined: 24 Nov 2007
Posts: 774

PostPosted: Thu Feb 26, 2009 4:09 pm    Post subject: Reply with quote

Actually, you offer 16 functions from the libary, and so far I have 65 methods from MSXML implemented.

But I'm supporting more than just XML Documents, as well--I'm supporting all object types supported by MSXML.

Thanks again for these functions, however!
_________________
Ben

My Trac projects
My Wiki
[Broken] - My music
Back to top
View user's profile Send private message
Display posts from previous:   
Reply to topic    AutoHotkey Community Forum Index -> Scripts & Functions All times are GMT
Page 1 of 1

 
Jump to:  
You can post new topics in this forum
You can reply to topics in this forum


Powered by phpBB © 2001, 2005 phpBB Group