AutoHotkey Community

It is currently May 27th, 2012, 11:02 am

All times are UTC [ DST ]




Post new topic This topic is locked, you cannot edit posts or make further replies.  [ 417 posts ]  Go to page Previous  1, 2, 3, 4, 5, 6, 7, 8 ... 28  Next
Author Message
 Post subject: It Works
PostPosted: November 8th, 2007, 7:27 am 
Thank you Titan,

now it works.


Report this post
Top
  
Reply with quote  
 Post subject: It doesn't work
PostPosted: November 25th, 2007, 12:33 pm 
Offline

Joined: November 25th, 2006, 9:02 am
Posts: 13
Getting data out of a xml file structure like that:

Code:
<pwlist>
  <pwentry>
    <title>mytitle01</title>
    <username>myuser01<username/>
    <password/>mypw01<password/>
  </pwentry>
  <pwentry>
    <title>mytitle02</title>
    <username>myuser02<username/>
    <password/>mypw02<password/>
  </pwentry>
...


I tested XPath with 2 different lines of code.

Line 1
Code:
info := xpath(xml, "/pwlist/pwentry[2]/password/text()")

Line 2
Code:
info := xpath(xml, "/pwlist/pwentry[title=mytitle02]/password/text()")


Line1 works as expected and info contains "mypw02".
With Line2, info contains "".
Could you please explain to me these 2 different results?
Thanks


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: November 25th, 2007, 12:47 pm 
Offline
User avatar

Joined: August 11th, 2004, 1:47 am
Posts: 5347
Location: UK
Your XML file is invalid, <username/> is not a proper closing tag and <password/> is a fused node and should not have any text after it.

_________________
GitHubScriptsIronAHK Contact by email not private message.


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: November 25th, 2007, 1:05 pm 
Offline

Joined: November 25th, 2006, 9:02 am
Posts: 13
I'm sorry... bad copy/edit/paste. xml file looks like:

Code:
<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<pwlist>
<pwentry>
   <title>mytitle01</title>
   <username></username>
   <url></url>
   <password>mypw01</password>
</pwentry>
<pwentry>
   <title>mytitle02</title>
   <username></username>
   <url></url>
   <password>mypw02</password>
</pwentry>
...


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: November 25th, 2007, 1:18 pm 
Offline
User avatar

Joined: August 11th, 2004, 1:47 am
Posts: 5347
Location: UK
Thats an outstanding bug which I will fix soon.

_________________
GitHubScriptsIronAHK Contact by email not private message.


Report this post
Top
 Profile  
Reply with quote  
PostPosted: November 28th, 2007, 7:56 pm 
Offline

Joined: November 28th, 2007, 7:17 pm
Posts: 2
Location: Edinburgh Scotland
Titan,
I am having an issue with changing code written in Xpath 1.03 to Xpath 2.06a.
How do I get the following code that was working in 1.03 to work with 2.06a.
Code:
#Include XPath.ahk ;Was using XPath 1.03.ahk
; Update for XPath 2.06a
sXMLFullPath = %A_ComputerName%-%A_NowUTC%.xml
xpath_load(xCapture)
XPath(xCapture, "/ExploratoryTesting[+1]")
XPath(xCapture, "/ExploratoryTesting/ComputerName[+1]", A_ComputerName)
XPath(xCapture, "/ExploratoryTesting/ComputerName/NowUTC[+1]", A_NowUTC)
xpath_save(xCapture, sXMLFullPath)

/*
The follow works with XPath 1.03
sXMLFullPath = %A_ComputerName%-%A_NowUTC%.xml
XMLDoc(xCapture)
XPath(xCapture, "/ExploratoryTesting[+1]")
XPath(xCapture, "/ExploratoryTesting/ComputerName[+1]", A_ComputerName)
XPath(xCapture, "/ExploratoryTesting/ComputerName/NowUTC[+1]", A_NowUTC)
XMLDoc(sXMLFullPath, xCapture)
*/

XPath 1.03 XML Results:
Code:
  <?xml version="1.0" encoding="iso-8859-1" ?>
- <ExploratoryTesting>
- <ComputerName>
  KIWIVISTA
  <NowUTC>20071128182743</NowUTC>
  </ComputerName>
  </ExploratoryTesting>

XPath 2.06a XML Results:
Code:
  <?xml version="1.0" encoding="iso-8859-1" ?>
  <ExploratoryTesting>KIWIVISTA</ExploratoryTesting>

Other system details: Auto Hotkey version 1.0.47.04 running on Vista Enterprise OS.

Also some feedback on: xpath_save() function
I think line 267...
Code:
FileDelete, %doc% ; delete existing file

shoud be (as it not deleting the XML file).
Code:
FileDelete, %src% ; delete existing file

and the XML saves an extra white space at the end of the file, therefore add at line 264 (XML not read into IE7)
Code:
StringTrimRight, xml, xml, 1

Look forward to your reply :wink:

_________________
C.S.


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: November 28th, 2007, 8:31 pm 
Offline
User avatar

Joined: August 11th, 2004, 1:47 am
Posts: 5347
Location: UK
Hi, all you need to do is add /text() to the end of your expressions (i.e. ".../NowUTC[+1]/text(), A_NowUTC)". Without them xpath tries to replace the newly created XML node ("<NowUTC></NowUTC>") with the value of A_NowUTC. I hope this makes sense.

Thanks for the bug report, I will fix it for the next update (whenever I get around to that).

_________________
GitHubScriptsIronAHK Contact by email not private message.


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: November 29th, 2007, 10:21 am 
Offline

Joined: November 28th, 2007, 7:17 pm
Posts: 2
Location: Edinburgh Scotland
Thank you, the following code works with XPath 2.06a

Code:
#Include XPath.ahk
;XPath version 2.06a, Auto Hotkey 1.0.47.4
sXMLFullPath =%a and AutoA_ComputerName%-%A_NowUTC%.xml
xpath_load(xCapture)
XPath(xCapture, "/ExploratoryTesting[+1]/text()")
XPath(xCapture, "/ExploratoryTesting/ComputerName[+1]/text()", A_ComputerName)
XPath(xCapture, "/ExploratoryTesting/ComputerName/NowUTC[+1]/text()", A_NowUTC)
xpath_save(xCapture, sXMLFullPath)

_________________
C.S.


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: December 18th, 2007, 12:09 am 
Offline

Joined: November 24th, 2007, 9:07 pm
Posts: 774
Every time I use xpath_save() to save my modified XML, I get an extra xml definition line added, and my old one gets its opening caret changed, like this:
Code:
<?xml version="1.0" encoding="iso-8859-1"?>
&lt;?xml version="1.0" encoding="iso-8859-1"?>
<gameversions>
<game>
<title>Crysis</title>
</game>
</gameversions>


I had to add this line right before it checks for <?xml:
Code:
StringReplace, xml, xml,% "&lt;",<

That seems to have fixed it.

Edit: It's also appending to my file if I don't manually delete it before I call xpath_save... even though the function looks like it's supposed to delete the file.
This looks odd to me:
Code:
FileDelete, %doc% ; delete existing file
FileAppend, %xml%, %src% ; create new one

Shouldn't both FileDelete and FileAppend be working on the same file? I modified it like this and it works better:
Code:
FileDelete, %src% ; delete existing file
FileAppend, %xml%, %src% ; create new one


So I've essentially resolved my issues I suppose, but I'm surprised neither of these issues have been apparent before?

It also appears the documentation for this function is out of date.

I can't use your example in the xpath() document to save my file (eg. the variables are switched) and I can't use the example in xpath() to load my file (specifying the path as the first and only parameter). I figured it out pretty quickly, but it added some moments of confusion.

_________________
Ben

My Trac projects
My Wiki
[Broken] - My music


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: December 18th, 2007, 12:52 am 
Offline

Joined: November 24th, 2007, 9:07 pm
Posts: 774
A couple more issues:
This is returning the raw XML source rather than the position:
Code:
curGamePos := xpath(VersionXML, "/gameversions/game[title=" . ThisTitle . "]/position()")


When I try to create new nodes:
Code:
xpath(VersionXML, "/gameversions/game[+1]/title[+1]", ThisTitle)


It's simple placing the raw text I add (contents of ThisTitle) rather than the text wrapped in the new XML nodes. So it just appends the text right where it would normally start the new tags.

I'm using Vista 64-bit but I don't really see that mattering in this case :?

_________________
Ben

My Trac projects
My Wiki
[Broken] - My music


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: December 18th, 2007, 9:12 am 
Offline
User avatar

Joined: August 11th, 2004, 1:47 am
Posts: 5347
Location: UK
bmcclure wrote:
When I try to create new nodes:
Code:
xpath(VersionXML, "/gameversions/game[+1]/title[+1]", ThisTitle)
Use /gameversions/game[+1]/title[+1]/text()

bmcclure wrote:
This is returning the raw XML source rather than the position:
Code:
curGamePos := xpath(VersionXML, "/gameversions/game[title=" . ThisTitle . "]/position()")
Could you post a sample of your XML file I can try this with?

Thanks for the bugs you mentioned earlier with the docs and XML declaration issue. I will look into them tonight.

_________________
GitHubScriptsIronAHK Contact by email not private message.


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: December 18th, 2007, 10:28 pm 
Offline
User avatar

Joined: August 11th, 2004, 1:47 am
Posts: 5347
Location: UK
Update 2.10a should resolve those issues.

_________________
GitHubScriptsIronAHK Contact by email not private message.


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: December 19th, 2007, 12:49 am 
Offline

Joined: November 24th, 2007, 9:07 pm
Posts: 774
Your text() suggestion partially worked, thanks!.

Note that the documentation lists this in the examples:
Code:
xpath(feed, "/rss/channel[+1]/title[+1]", "second") ; rss > channel > title


(In my example) Adding text() to the end of it actually adds new title nodes now, but not the game nodes. It seems I'll have to split it up into two commands, one to create the game node and one to add the title node to it. I'll post back if that works.

Is 2.10a released? It looks like 2.05 is listed on your site. Thanks!

Update: I changed my code to look like this:
Code:
xpath(VersionXML, "/gameversions/game[+1]/text()"," ")
xpath(VersionXML, "/gameversions/game/last()/title[+1]/text()", ThisTitle)

Only now I experience the odd behavior of every loop in the iteration which uses this code taking progressively longer and longer, until around the 20th time I get a max memory used error. Did I split the command up wrong? I'm trying to add a new <game> node then a new <title> node inside that.

_________________
Ben

My Trac projects
My Wiki
[Broken] - My music


Last edited by bmcclure on December 19th, 2007, 1:00 am, edited 1 time in total.

Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: December 19th, 2007, 12:56 am 
Offline
User avatar

Joined: August 11th, 2004, 1:47 am
Posts: 5347
Location: UK
Yep, a link to 2.10a is on my first post here.

_________________
GitHubScriptsIronAHK Contact by email not private message.


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: December 19th, 2007, 1:12 am 
Offline

Joined: November 24th, 2007, 9:07 pm
Posts: 774
OK, now I'm getting somewhere with this function!

It now creates proper XML files (albiet they are only one line now)

The problem is that it's still putting an invalid copy of my declaration back into the final file, only now at the end.

Here's what I started with:
Code:
<?xml version="1.0" encoding="iso-8859-1"?>


I loaded the file, added new nodes, and wrote the file again, and this is what comes out:
Code:
<?xml version="1.0" encoding="iso-8859-1"?>
<gameversions><game><title>Halo 2</title></game><game><title>Medal of Honor: Airborne</title></game><game><title>Neverwinter Nights 2</title></game><game><title>Quake 4</title></game><game><title>Shadowrun</title></game><game><title>Stranglehold</title></game><game><title>Viva Pinata</title></game><game><title>Gears of War</title></game><game><title>Hellgate: London Multiplayer</title></game><game><title>Hellgate: London Single Player</title></game><game><title>The Witcher</title></game><game><title>TimeShift</title></game><game><title>The Elder Scrolls 4: Oblivion</title></game><game><title>Crysis</title></game><game><title>Sam and Max Episode 3</title></game><game><title>Unreal Tournament 3</title></game><game><title>BlackSite: Area 51</title></game></gameversions> &lt;?xml version="1.0" encoding="iso-8859-1"?>


Sorry for the length, but notice my original declaration line at the end of the file with an invalid starting caret. I'm not sure how that caret is being converted, but I'm not doing it in my code.

_________________
Ben

My Trac projects
My Wiki
[Broken] - My music


Report this post
Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic This topic is locked, you cannot edit posts or make further replies.  [ 417 posts ]  Go to page Previous  1, 2, 3, 4, 5, 6, 7, 8 ... 28  Next

All times are UTC [ DST ]


Who is online

Users browsing this forum: iDrug and 61 guests


You can post new topics in this forum
You can reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Powered by phpBB® Forum Software © phpBB Group