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 

xpath v3 - read and write XML documents with XPath syntax
Goto page Previous  1, 2, 3, 4 ... 19, 20, 21  Next
 
Post new topic   Reply to topic    AutoHotkey Community Forum Index -> Scripts & Functions
View previous topic :: View next topic  
Author Message
ChaoticPi
Guest





PostPosted: Fri Aug 03, 2007 8:58 pm    Post subject: Reply with quote

Ok, I am not fully understanding something, so maybe you can help. I am attempting to get data out of a Form file (*.XFT), which is implemented in XML:

This is not the begining of the file, the file is very big, but what I am interested in, is getting the HeightInChars, and WidthInChars from the given field.
Code:
    <field name="SCHEDULE" y="191.56mm" x="25.4mm" w="170.39mm" h="11.38mm">
     <value>
      <exData/>
     </value>
     <ui>
      <textEdit>
       <extras type="application/x-active-x">
        <integer name="WantReturns">-1</integer>
        <integer name="Occurrence">1</integer>
        <text name="MapIndex">00033</text>
        <text name="EXTENDED_CodeBase">RichEdit.dll</text>
        <integer name="LabelForeColor"/>
        <integer name="SystemLabelForeColor">-2147483640</integer>
        <integer name="LabelBackColor">16777215</integer>
        <integer name="SystemLabelBackColor">-2147483643</integer>
        <integer name="SystemInactiveBackColor">-2147483633</integer>
        <integer name="ScrollLabel"/>
        <integer name="BackColor">16777215</integer>
        <integer name="SystemBackColor">-2147483643</integer>
        <integer name="PrintBackColor">16777215</integer>
        <text name="Tab">1270;Left</text>
        <float name="RightIndent"/>
        <float name="SpaceBefore"/>
        <float name="SpaceAfter"/>
        <integer name="LineSpacing">4</integer>
        <float name="LineSpacingPoints">12.5</float>
        <float name="SpecialIndentBy"/>
        <text name="SpecialIndent">(none)</text>
        <integer name="HeightInChars">2</integer>
        <integer name="WidthInChars">79</integer>
        <integer name="SaveBorderPlacement">-1</integer>
        <text name="CalcEngine">Default</text>
        <integer name="BorderPlacement">3</integer>
        <integer name="StartingColor">13160660</integer>
        <integer name="MaxChars">240</integer>
       </extras>
      </textEdit>
     </ui>
     <caption>
      <font size="9.75pt" typeface="Courier"/>
     </caption>
     <font size="9.75pt" typeface="Courier">
      <fill>
       <color value="0,0,0"/>
      </fill>
     </font>
     <calculate override="warning"/>
     <border hand="right">
      <edge thickness="0.26mm" stroke="lowered">
       <color value="0,0,0"/>
      </edge>
      <fill>
       <solid/>
       <color value="212,208,200"/>
      </fill>
     </border>
     <margin topInset="0.26mm" rightInset="0.26mm" leftInset="0.26mm" bottomInset="0.26mm"/>
    </field>


I am getting the field name in my ListView correctly, I just can't seem to get any information based on attribute values, when I try other variations, it returns the whole field node.

The portion of my *.ahk script that matters looks like this:
Code:

xml := XmlDoc(XFTFile) ; load the xft file
fields := XPath(xml, "/xfa/template/subform/area/field")
Loop, Parse, fields, `, ; the delimiter is always a comma
   LV_Add(XPath(fields, "/xfa/template/subform/area/field"[ . A_Index . "]/@name[1]/text()"))
StringSplit, fields_, fields, `, ; the variable 'fields_0' contains the number of selected elements
LV_Delete()
Loop, %fields_0% {
;This gets the field name and works correctly
    LV_Add("",XPath(fields_%A_Index%, "field/@name[1]/text()"))

;This is supposed to get the HeightInChars, and won't work
    LV_Add("",XPath(fields_%A_Index%, "field/ui/textEdit/extras/integer[@name='HeightInChars']/text()"))
Back to top
Titan



Joined: 11 Aug 2004
Posts: 5048
Location: imaginationland

PostPosted: Fri Aug 03, 2007 10:23 pm    Post subject: Reply with quote

You made a few mistakes - firstly you should only operate on XmlDoc() vars and the second parameter in LV_Add() shows the values. Here's what it should look like:

Code:
xml := XmlDoc(XFTFile) ; load the xft file
fields := XPath(xml, "/xfa/template/subform/area/field") ; use this value to parse each field set

Loop, Parse, fields, `, ; for each:
   LV_Add("", XPath(xml, "/xfa/template/subform/area/field[" . A_Index . "]/@name/text()")) ; get field name

LV_Add("", XPath(xml, "/xfa/template/subform/area/field/@name/text()")) ; get all field names as a comma seperated list
LV_Add("", XPath(xml, "/xfa/template/subform/area/field/ui/textEdit/extras/integer[@name=HeightInChars]/text()")) ; get all HeightInChars integer values

_________________

RegExReplace("irc.freenode.net/ahk", "^(?=(.(?=[\0-r\[]*((?<=\.).))))(?:[c-\x73]{2,8}(\S))+((2)|\b[^\2-]){2}\D++$", "$u3$1$3$4$2")
Back to top
View user's profile Send private message Visit poster's website
Avalon



Joined: 11 May 2007
Posts: 28

PostPosted: Sun Aug 26, 2007 3:58 pm    Post subject: Reply with quote

I have a basic query regarding XML.

I want to use a xml file to store my hotkeys, and then to load them into a Listview. I created an xml file, but I'n not quite sure that this is a valid xml file, could you please have a look into it, and advise if possibe?

Thank you in advance! Smile

Here is one item and the leading part of my xml file.
Code:
<?xml version="1.0" encoding="UTF-8"?>

<channel>
<title>REDFOG hotkey list</title>
<description>Hotkey list for REDFOG</description>
<author>Mate Gulyas</author>
</end>

<item>
<name>!mail</name>
<description>Standard mail reply form</description>
</item>

_________________
dA Profile
Back to top
View user's profile Send private message
Ace_NoOne



Joined: 10 Oct 2005
Posts: 333
Location: Germany

PostPosted: Sun Aug 26, 2007 4:04 pm    Post subject: Reply with quote

Doesn't look quite right, Avalon; you're missing a root element, and the </end> tag should probably be </channel>:
Code:
<?xml version="1.0" encoding="UTF-8" ?>

<root>

<channel>
    <title>REDFOG hotkey list</title>
    <description>Hotkey list for REDFOG</description>
    <author>Mate Gulyas</author>
</channel>

<item>
    <name>!mail</name>
    <description>Standard mail reply form</description>
</item>

</root>

There should also be a well-formedness checker out there (Tidy?) - although proper validation requires a DTD or Schema.
_________________
Improving my world, one script at a time.
Join the AutoHotkey IRC channel: irc.freenode.net #autohotkey
Back to top
View user's profile Send private message
Avalon



Joined: 11 May 2007
Posts: 28

PostPosted: Sun Aug 26, 2007 5:22 pm    Post subject: Reply with quote

Ace_NoOne wrote:
There should also be a well-formedness checker out there (Tidy?) - although proper validation requires a DTD or Schema.


Thanks, I reformatted my XML file, but my code is still not working.

Here is my XML file:
Code:
<?xml version="1.0" encoding="UTF-8" ?>
<root>
<channel>
    <title>Hotkey list</title>
    <description>Hotkey list for SC script</description>
    <author>SG</author>
</channel>

<item>
<name>!mail</naquotme>
<description>Standard mail reply form</description>
<scope>Lotus Notes</scope>
</item>

<item>
<name>!scont</name>
<description>Subject of Contact Exception email</description>
<scope>Lotus Notes</scope>
</item>

<item>
<name>!cont</name>
<description>Body of Contact Exception email</description>
<scope>Lotus Notes</scope>
</item>

<item>
<name>mll</name>
<description>Cleans up ICT Incident report mail</description>
<scope></scope>
</item>

<item>
<name>pwmll</name>
<description>Cleans up ICT password reset mail</description>
<scope>global</scope>
</item>

<item>
<name>!regards</name>
<description>Best regards, [Your name]</description>
<scope>Lotus Notes</scope>
</item>

<item>
<name>tel</name>
<description>telephone number</description>
<scope>global</scope>
</item>

<item>
<name>Pw</name>
<description>Password</description>
<scope></scope>
</item>

<item>
<name>pw</name>
<description>password</description>
<scope></scope>
</item>

</root>


Any idea how I should parse this whole file into a ListView?
I'm thinking something like this: (it still misses the exit condition of the Loop, and I have a guess that my XML reading part is a total deadend. Sad
Code:

Loop
{
xml := XmlDoc("hotkeys.xml")
hotkeys := XPath(xml, "/channel/item[%A_Index%]/name/text()")
description := XPath(xml, "/channel/item[%A_Index%]/description/text()")
scope := XPath(xml, "/channel/item[%A_Index%]/scope/text()")
LV_Add(,%hotkeys%,%description%,%scope%)
}

_________________
dA Profile
Back to top
View user's profile Send private message
Titan



Joined: 11 Aug 2004
Posts: 5048
Location: imaginationland

PostPosted: Sun Aug 26, 2007 5:46 pm    Post subject: Reply with quote

There are a few errors in your script, see FAQ: When exactly are variable names enclosed in percent signs?

Here is a working example:

Code:
#Include XPath.ahk

Gui, Add, ListView, w400 r10, Hotkeys|Description|Scope
Gui, Show, , Example

xml := XmlDoc("hotkeys.xml") ; this only needs to be loaded once

Loop
{
   If XPath(xml, "/root/item[" . A_Index . "]") = ""
      Break
   hotkeys := XPath(xml, "/root/item[" . A_Index . "]/name/text()")
   description := XPath(xml, "/root/item[" . A_Index . "]/description/text()")
   scope := XPath(xml, "/root/item[" . A_Index . "]/scope/text()")
   LV_Add("", hotkeys, description, scope)
}

Return

GuiClose:
ExitApp


There there is also a typo in your XML, </naquotme> should be </name>.
_________________

RegExReplace("irc.freenode.net/ahk", "^(?=(.(?=[\0-r\[]*((?<=\.).))))(?:[c-\x73]{2,8}(\S))+((2)|\b[^\2-]){2}\D++$", "$u3$1$3$4$2")
Back to top
View user's profile Send private message Visit poster's website
Avalon



Joined: 11 May 2007
Posts: 28

PostPosted: Sun Aug 26, 2007 11:29 pm    Post subject: Reply with quote

Titan wrote:
There are a few errors in your script, see FAQ:

Yeah, now i can see how many errors I had. :S I'm very new to this progamming thing, I usually make some very lame mistakes. Smile

I could manage to get to a working version, however I had to use the simple GUI, as with Gui, 2: wasn't working. So now my simple GUI is the one which uses the ListView, and the previous is the second ( GUI, 2: ).

Thanks for the help anyway, I will try to do more complicated XMLs, I find it very useful. Smile
_________________
dA Profile
Back to top
View user's profile Send private message
fincs



Joined: 05 May 2007
Posts: 82
Location: Seville, Spain

PostPosted: Mon Aug 27, 2007 6:27 pm    Post subject: Reply with quote

I have a trouble with a XML file that looks like this
Code:
<docu number="2">
     <function name="add">
        <syntax>number1, number2[, number3=0]</syntax>
    </function>
    <function name="pow">
        <syntax>base, exponent</syntax>
    </function>
</docu>


And I want to read each <function> tag.
My code:
Code:
xmlbase := XmlDoc("test.xml")
nfuncs := XPath(xmlbase, "/docu/@number/text()")
Loop, %nfuncs%
{
   funcname := XPath(xmlbase, "/docu/function[" A_Index "]/@name/text()") ; Get function's name
   syntax := XPath(xmlbase, "/docu/function[" A_Index "]/syntax/text()")
   MsgBox %funcname%`n%syntax%
}


I get MsgBoxed two times: one with add,pow and other with nothing. Can you tell me what's wrong in my code?
_________________
Sorry my poor English
Fincs

MsgBox % RegExReplace("Fernando Incs.", "^(\w).*\s+I(\w{3})\w*\.$", "$1i$2")
Back to top
View user's profile Send private message
Titan



Joined: 11 Aug 2004
Posts: 5048
Location: imaginationland

PostPosted: Mon Aug 27, 2007 7:32 pm    Post subject: Reply with quote

It works fine for me. You should use StringReplace, syntax, syntax, ,, `,, All before the message box to unescape the syntax variable. Are you sure you have the latest version of AutoHotkey and XPath.ahk?
_________________

RegExReplace("irc.freenode.net/ahk", "^(?=(.(?=[\0-r\[]*((?<=\.).))))(?:[c-\x73]{2,8}(\S))+((2)|\b[^\2-]){2}\D++$", "$u3$1$3$4$2")
Back to top
View user's profile Send private message Visit poster's website
engunneer



Joined: 30 Aug 2005
Posts: 6772
Location: Pacific Northwest, US

PostPosted: Mon Aug 27, 2007 9:32 pm    Post subject: Reply with quote

Titan,

can you rename the XMLDoc function to XPath_XMLDoc, (or add a single line wrapper to do the same)?

It will then be StdLib compliant when called XPath.ahk

Thanks
_________________
Unless otherwise noted, all code is untested.
Common Answers: 1.(Loops, Viruses, etc.) 2. Search 3.RTFM
Back to top
View user's profile Send private message Visit poster's website
Titan



Joined: 11 Aug 2004
Posts: 5048
Location: imaginationland

PostPosted: Mon Aug 27, 2007 9:44 pm    Post subject: Reply with quote

I'm not too keen on prefixes but I'll consider it for the next version.
_________________

RegExReplace("irc.freenode.net/ahk", "^(?=(.(?=[\0-r\[]*((?<=\.).))))(?:[c-\x73]{2,8}(\S))+((2)|\b[^\2-]){2}\D++$", "$u3$1$3$4$2")
Back to top
View user's profile Send private message Visit poster's website
fincs



Joined: 05 May 2007
Posts: 82
Location: Seville, Spain

PostPosted: Mon Aug 27, 2007 10:14 pm    Post subject: Reply with quote

@Titan:

I'm using latest version of AutoHotkey and XPath.
Oh, I forget to say that the XML file is auto-generated when I read and parse a file (and I don't use XmlDoc, so the XML file aren't preprocessed), so the commas are the trouble. Well, problem solved! Thanks!
_________________
Sorry my poor English
Fincs

MsgBox % RegExReplace("Fernando Incs.", "^(\w).*\s+I(\w{3})\w*\.$", "$1i$2")
Back to top
View user's profile Send private message
Titan



Joined: 11 Aug 2004
Posts: 5048
Location: imaginationland

PostPosted: Tue Aug 28, 2007 1:55 pm    Post subject: Reply with quote

I'm glad you got it working.

By the way xpath 2.0 will be out soon and I've published the documentation for it (best viewed in Firefox). This version parses nodes a lot faster than msxml3 when using the current COM wrappers. Hopefully if and when these wrappers are improved I plan to write an XML DOM set of functions complete with XSLT.
_________________

RegExReplace("irc.freenode.net/ahk", "^(?=(.(?=[\0-r\[]*((?<=\.).))))(?:[c-\x73]{2,8}(\S))+((2)|\b[^\2-]){2}\D++$", "$u3$1$3$4$2")
Back to top
View user's profile Send private message Visit poster's website
Titan



Joined: 11 Aug 2004
Posts: 5048
Location: imaginationland

PostPosted: Wed Aug 29, 2007 12:15 am    Post subject: Reply with quote

xpath 2.0 alpha released w00t!
Users are requested to test scripts for compatibility and contact me if they notice any bugs etc.
_________________

RegExReplace("irc.freenode.net/ahk", "^(?=(.(?=[\0-r\[]*((?<=\.).))))(?:[c-\x73]{2,8}(\S))+((2)|\b[^\2-]){2}\D++$", "$u3$1$3$4$2")
Back to top
View user's profile Send private message Visit poster's website
Sean



Joined: 12 Feb 2007
Posts: 1331

PostPosted: Wed Aug 29, 2007 12:35 am    Post subject: Reply with quote

Titan wrote:
This version parses nodes a lot faster than msxml3 when using the current COM wrappers.

You're really obsessed with the speed. Anyway, I don't think the COM could compete with the functions here no matter how much optimized is the code. Even neither with the VTBL interface (:why didn't you try VTBL anyway. You said you were gonna try VTBL). Why? It always have to convert the (ANSI) string to Unicode string, then, have to allocate and fill another buffer via SysAllocString.

Just test it if you're concerned after optimizing Invoke() as possible as you can.
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    AutoHotkey Community Forum Index -> Scripts & Functions All times are GMT
Goto page Previous  1, 2, 3, 4 ... 19, 20, 21  Next
Page 3 of 21

 
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