 |
AutoHotkey Community Let's help each other out
|
| View previous topic :: View next topic |
| Author |
Message |
majkinetor
Joined: 24 May 2006 Posts: 4511 Location: Belgrade
|
Posted: Fri Jan 04, 2008 9:27 pm Post subject: Microsoft.XMLDOM |
|
|
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 |
|
 |
COM TTS Guest
|
Posted: Fri Jan 04, 2008 10:38 pm Post subject: |
|
|
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
|
|
| Back to top |
|
 |
derRaphael
Joined: 23 Nov 2007 Posts: 841 Location: ~/.
|
Posted: Fri Jan 04, 2008 11:14 pm Post subject: |
|
|
thank you majkinetor, this just comes in handy _________________
All scripts, unless otherwise noted, are hereby released under CC-BY |
|
| Back to top |
|
 |
pantagruel
Joined: 08 Oct 2006 Posts: 96 Location: denmark
|
Posted: Mon Oct 13, 2008 7:30 pm Post subject: How do I append, cloneNodes |
|
|
| 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 |
|
 |
Lexikos
Joined: 17 Oct 2006 Posts: 7295 Location: Australia
|
Posted: Wed Oct 15, 2008 7:26 am Post subject: |
|
|
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 |
|
 |
pantagruel
Joined: 08 Oct 2006 Posts: 96 Location: denmark
|
Posted: Wed Oct 15, 2008 7:57 am Post subject: thanks, so this means.. |
|
|
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 |
|
 |
Lexikos
Joined: 17 Oct 2006 Posts: 7295 Location: Australia
|
Posted: Wed Oct 15, 2008 8:26 am Post subject: |
|
|
Your understanding seems sound.  |
|
| Back to top |
|
 |
amokoura
Joined: 14 Nov 2006 Posts: 15
|
Posted: Tue Jan 06, 2009 7:57 am Post subject: |
|
|
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 |
|
 |
bmcclure
Joined: 24 Nov 2007 Posts: 774
|
Posted: Thu Feb 26, 2009 1:32 am Post subject: |
|
|
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  _________________ Ben
My Trac projects
My Wiki
[Broken] - My music |
|
| Back to top |
|
 |
Guest
|
Posted: Thu Feb 26, 2009 7:54 am Post subject: |
|
|
awesome, thanks very much!  |
|
| Back to top |
|
 |
majkinetor ! Guest
|
Posted: Thu Feb 26, 2009 1:48 pm Post subject: |
|
|
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
|
Posted: Thu Feb 26, 2009 4:09 pm Post subject: |
|
|
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 |
|
 |
|
|
You can post new topics in this forum You can reply to topics in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|