 |
AutoHotkey Community Let's help each other out
|
| View previous topic :: View next topic |
| Author |
Message |
Titan
Joined: 11 Aug 2004 Posts: 5382 Location: /b/
|
Posted: Wed Apr 12, 2006 6:07 pm Post subject: |
|
|
In version 1.02 I improved the indentation and removed the extra space that was inserted for the parsing. I also introduced XMLWriteEx() in the doc - its simply a small extension that allows you to make multiple XML calls at once. _________________
 |
|
| Back to top |
|
 |
Ace_NoOne
Joined: 10 Oct 2005 Posts: 333 Location: Germany
|
Posted: Sat Apr 15, 2006 6:38 pm Post subject: |
|
|
| Thanks a bunch, Titan - can't wait to try it out! |
|
| Back to top |
|
 |
chris-b Guest
|
Posted: Mon Apr 17, 2006 6:44 pm Post subject: |
|
|
Hey Titan!
Your XMLWrite really rocks but I have a little problem.
When I run this:
| Code: | XMLWrite(configFile, "psiconf.preferences.sound.onevent.message", "%psiDir%/chat2.wav")
|
I get this result:
| Code: | <psiconf>
<preferences>
<sound>
<onevent>
<message>%psiDir%/chat2.wav</message>
</onevent>
</sound>
</preferences>
</psiconf> |
Why is %psiData% not translated to the real path i used in the script? Is it an error in the function or am I doing anything wrong?
Thx, Chris |
|
| Back to top |
|
 |
PhiLho
Joined: 27 Dec 2005 Posts: 6721 Location: France (near Paris)
|
Posted: Mon Apr 17, 2006 6:53 pm Post subject: |
|
|
| Code: | | XMLWrite(configFile, "psiconf.preferences.sound.onevent.message", psiDir . "/chat2.wav") |
_________________
vPhiLho := RegExReplace("Philippe Lhoste", "^(\w{3})\w*\s+\b(\w{3})\w*$", "$1$2") |
|
| Back to top |
|
 |
Guest
|
Posted: Mon Apr 17, 2006 7:15 pm Post subject: |
|
|
Now it works. Big thanks!
Chris |
|
| Back to top |
|
 |
AHKnow* Guest
|
Posted: Tue Apr 18, 2006 8:10 am Post subject: |
|
|
| XMLQuery??? |
|
| Back to top |
|
 |
Titan
Joined: 11 Aug 2004 Posts: 5382 Location: /b/
|
Posted: Tue Apr 18, 2006 10:00 am Post subject: |
|
|
I've been planning to make an extra XML function after this. We have the reader and the writer, so having a query function to gather extra information would be great. Have you any ideas on what features it should include? _________________
 |
|
| Back to top |
|
 |
Ace_NoOne
Joined: 10 Oct 2005 Posts: 333 Location: Germany
|
Posted: Tue Apr 18, 2006 10:46 am Post subject: |
|
|
That'd be awesome, Titan!
I guess we've discussed that before in the XmlRead thread - I'd have to dig through that again cuz I can't really remember what would be required for such a function (I don't have much time for coding these days, and you'd have to be actively working on a project to really figure out which functionality would be needed).
Also, you should then comprise all three functions (XmlRead, XmlWrite and XmlQuery) into a single file/thread called XmlParser.
I'd be happy to help you documenting the functions then (plus most of the work has already done by you with the "Usage Notes" for XmlRead and the documentation for XmlWrite)! |
|
| Back to top |
|
 |
Titan
Joined: 11 Aug 2004 Posts: 5382 Location: /b/
|
Posted: Tue Apr 18, 2006 9:31 pm Post subject: |
|
|
In version 1.03 you can remove elements by specifying a blank data parameter for it. This is because blank elements are invalid (using a space/ is the suggested workaround).
| Ace_NoOne wrote: | | Also, you should then comprise all three functions (XmlRead, XmlWrite and XmlQuery) into a single file/thread called XmlParser. | Doing this would be like merging IniRead, IniWrite and IniDelete. This is a bad move because not only does it make it more confusing but it also requires more code, requires one extra parameter and takes more time due to overloading. Since each function (Write, Read, Query) are very different in how they process the XML document, it doesn't make sense to combine them.
Regarding the XMLQuery, I'll have a look at W3's specs and try to code something. I'm happy because I've managed to get the writer and reader done atleast Thanks for your continued support. _________________
 |
|
| Back to top |
|
 |
Ace_NoOne
Joined: 10 Oct 2005 Posts: 333 Location: Germany
|
Posted: Tue Apr 18, 2006 10:00 pm Post subject: |
|
|
| Titan wrote: | | In version 1.03 you can remove elements by specifying a blank data parameter for it. This is because blank elements are invalid (using a space/ is the suggested workaround). | Blank elements are not invalid in XML, or are they? Both <element></element> and <element /> are just fine AFAIK.
But I might have misunderstood what you actually meant...
| Titan wrote: | | Ace_NoOne wrote: | | Also, you should then comprise all three functions (XmlRead, XmlWrite and XmlQuery) into a single file/thread called XmlParser. | Doing this would be like merging IniRead, IniWrite and IniDelete. This is a bad move because not only does it make it more confusing but it also requires more code, requires one extra parameter and takes more time due to overloading. Since each function (Write, Read, Query) are very different in how they process the XML document, it doesn't make sense to combine them. | Sorry, you misunderstood; I didn't mean combining them into a single function, but putting all three functions into a single file so you'd only have to include that one file called XmlParser in your scripts (which is what I do already).
Plus creating a single thread (once eveything's more or less final) for the whole XML parser issue would make it easier for newcomers to find their way around.
| Titan wrote: | | Regarding the XMLQuery, I'll have a look at W3's specs and try to code something. | I'm currently trying to remember what I'd wished for back when I was learning to use XmlRead - so maybe you should start a new thread for this function to collect suggestions?
However, see below!
| Titan wrote: | I'm happy because I've managed to get the writer and reader done atleast Thanks for your continued support. | You should well be proud of what you've accomplished - these functions are not only a great achievement for yourself, but also an extremely useful extension of AHK's capabilities! (I wish Chris would comment on this somehow *hint* *nudge* )
So take a break and don't worry about the next step for now.  |
|
| Back to top |
|
 |
Ace_NoOne
Joined: 10 Oct 2005 Posts: 333 Location: Germany
|
Posted: Wed Apr 19, 2006 10:43 am Post subject: |
|
|
Uh-oh - found two more problems (one minor, the other possibly major):
What I want to do is to dynamically create a HTML list of URLs. So I use the following code: | Code: | #SingleInstance Force
#NoEnv
; use Titan's XML parser
#Include XmlParser.ahk
storeFile = test.html
HtmlTree_item = html.body.ul.li
i = 3
Loop, %i%
{
; construct URL and description
URL = http://www.domain%A_Index%.com
desc = This is a link to domain %A_Index%.
; add a new link
errors += XmlWrite(storeFile, HtmlTree_item . "[+1].a", URL
, "attribute['href']", "IndT")
; add description to link - DEBUG: how to access the last(-used) item?
errors += XmlWrite(storeFile, HtmlTree_item . ".a", desc, "", "IndT")
; return errors if any
If errors > 0
MsgBox, There were %errors% errors in writing the URL(s) to file.
}
Run, Notepad %storeFile% | The output I get from this looks like this: | Code: |
<html>
<body>
<ul>
<li>
<a href="http://www.domain1.com">This is a link to domain 3.</a>
</li>
<li>
<a href="http://www.domain2.com"></a>
</li>
<li>
<a href="http://www.domain3.com"></a>
</li>
</ul>
</body>
</html>
| Let's start with the minor issue - too many line breaks. There's a blank line break at the top as well as at the bottom of the file. Whereas the latter might be okay or even intentional, I don't like the one at the top.
Also, there are blank lines in between the list items (LI tag). These even contain tabs ("Select all" to see them), which further bloats the file.
More importantly though, this example made me realize that XmlWrite also requires parts of what would be XmlQuery:
As you can see, I need to set the link (HREF attribute) as well as add the description (element data). This requries two separate runs; the first creates the new list item and adds the attribute. The second just needs to add the description to the previously created item.
However, there is no easy way to select this last item. Of course I could find out it's index using XmlRead, but that'd be quite complicated and inefficient...
That's why in the current state, only the first item actually gets a description (as I don't use any index when adding the description, so it's always the first item that's being accessed).
 |
|
| Back to top |
|
 |
Titan
Joined: 11 Aug 2004 Posts: 5382 Location: /b/
|
Posted: Wed Apr 19, 2006 11:05 am Post subject: |
|
|
This the way I done it, and it works perfectly: | Code: | store = %temp%\test.html ; the file - change extension to .xml to see structure in Internet Explorer below!
tree = html.body.ul.li ; the tree
ind = IndT ; the 'options'
i = 3 ; loop iterations
FileDelete, %store% ; delete any previous instance
Loop, %i% {
URL = http://www%A_Index%.google.com ; the URL
desc = This is link to item %A_Index% ; the description
errors += XMLWrite(store, tree . "[+1].a", desc, "", ind) ; new item to END of list
errors += XMLWrite(store, tree . "[" . A_Index - 1 . "].a", URL, "attribute['href']", ind) ; very LAST item's attribute
}
IfGreater, error, 0, MsgBox, 16, Errors, There were %errors% errors. ; if there were errors, let us know
Run, notepad.exe "%store%" ; open in notepad (to check spacing)
Run, iexplore.exe "file:///%store%" ; open in Internet Explorer (to check it worked)
WinWait, Internet Explorer, , 2 ; wait 2 secs for Internet Explorer to open
FileDelete, %store% ; delete the file
ExitApp |
The minor issue: I'll fix up the spacing in the next version thanks.
The major issue: You don't need XmlQuery for this because you can achieve the same result quite easily as I demonstrated above  _________________
 |
|
| Back to top |
|
 |
Ace_NoOne
Joined: 10 Oct 2005 Posts: 333 Location: Germany
|
Posted: Wed Apr 19, 2006 11:15 am Post subject: |
|
|
Clever workaround there, but that only works if the file is actually created in such a loop.
What, though, if there are already a number of items in the list and you just wanna add a few more (maybe even in the middle of the list ) - then you won't have the A_Index var to help you...
PS: Sorry for posting twice in a row up there, but they were separate issues so I didn't wanna mix them up. |
|
| Back to top |
|
 |
Titan
Joined: 11 Aug 2004 Posts: 5382 Location: /b/
|
Posted: Wed Apr 19, 2006 11:39 am Post subject: |
|
|
| Ace_NoOne wrote: | | Clever workaround there, but that only works if the file is actually created in such a loop. | That's true. XMLQuery will have a count feature to get the number of elements within a specified branch. In the meantime, you can easily get this number by doing the following: | Code: | Loop {
XMLRead(file, tree . "[" . A_Index - 1 . "]")
IfEqual, ErrorLevel, 1, Break
Else i++
}
MsgBox, There are %i% elements of %tree% |
| Ace_NoOne wrote: | What, though, if there are already a number of items in the list and you just wanna add a few more (maybe even in the middle of the list ) | It's only possible to add new items to the beginning or end of the list or replace existing ones (i.e. in the middle). There is currently no way of shifting down the list to add a new entry in the middle somewhere. In fact I have not seen any other XML writer that can do this. Again it would be good for XMLQuery to have a shift down/up feature but it can be done by other means too.
| Ace_NoOne wrote: | | PS: Sorry for posting twice in a row up there, but they were separate issues so I didn't wanna mix them up. | Honestly that's no problem  _________________
 |
|
| Back to top |
|
 |
Chris Site Admin
Joined: 02 Mar 2004 Posts: 10480
|
Posted: Wed Apr 19, 2006 11:54 am Post subject: |
|
|
| Ace_NoOne wrote: | | these functions are not only a great achievement for yourself, but also an extremely useful extension of AHK's capabilities! | Although I don't use XML for anything yet myself, I agree that it's important -- perhaps not too far below direct access to MySQL and other databases. When a standard library is created to be distributed with the program, XML functions like these should be one of the first things in it. |
|
| 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
|