despertar

Stuck writing a search engine for .xml

6 posts in this topic

#1 ·  Posted (edited)

Hi there!

I'm working on a piece of software for the family business in order to make inventory, stock, sales statistics etc. easier to maintain and control.

I'm currently stuck on the search engine part, trying to make a function that'll search a .xml file; more specifically the <Description> elements of a .xml file, using user input.

I then want the function to display the text from the sibling <Category>, <Type>, <Quantity> and <UOM> as well as the <Description> in a list view or something similar.

I was hoping to avoid asking for help, but thirteen hours in and a brain in complete meltdown due to lack of understanding, I feel it's time. I tried using code from this forum, playing around with _XMLDomWrapper, but I simply do not understand it enough to make it work. Usually I get by with looking at other people's code and through logic deduce what's doing what so I can apply it to my own code, but I'm out of my depth here.

I was able to make a function change one element of an XML file (though I was trying for hours to change all identical elements to no avail), but that's about it. 

The XML file in question:

<Varer>
    <Vare>
        <Category>Trelast</Category>
        <Type>Impregnert</Type>
        <Description>28 x 120 Impregnert Terrassebord</Description>
        <Quantity>5</Quantity>
        <UOM>Meter</UOM>
    </Vare>

    <Vare>
        <Category>Trelast</Category>
        <Type>Impregnert</Type>
        <Description>21 x 95 Impregnert Terrassebord</Description>
        <Quantity>5</Quantity>
        <UOM>Meter</UOM>
    </Vare>


    <Vare>
        <Category>Festemidler</Category>
        <Type>Skruer</Type>
        <Description>3,4 x 55 Terrasseskruer</Description>
        <Quantity>5</Quantity>
        <UOM>Esker</UOM>
    </Vare>

</Varer>

 

Does anyone have a similar function I could take a look at, or could point me to a guide of some kind?

Any help would be much appreciated!

Varer.xml

Edited by despertar

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

If all entries are 5 lines, and description is in the middle, then

#include<array.au3>
#include<file.au3>


_ArrayDisplay(_2LinesBeforeAndAfter("Varer.xml" , "Terrasseskruer")) ; 1 return
_ArrayDisplay(_2LinesBeforeAndAfter("Varer.xml" , "Terrassebord"))  ;2 returns
_ArrayDisplay(_2LinesBeforeAndAfter("Varer.xml" , "Terase"))  ;misspelled


Func _2LinesBeforeAndAfter($File , $sMatch)

Local $aArray
Local $aOut[0]
_FileReadToArray($File , $aArray)

For $i = 0 to ubound($aArray) - 1
    If stringinstr($aArray[$i] , $sMatch) Then
        _ArrayAdd($aOut , $aArray[$i - 2])
        _ArrayAdd($aOut , $aArray[$i - 1])
        _ArrayAdd($aOut , $aArray[$i])
        _ArrayAdd($aOut , $aArray[$i + 1])
        _ArrayAdd($aOut , $aArray[$i + 2])
        _ArrayAdd($aOut , "")
    EndIf
Next


If ubound($aOut) = 0 then $aOut = msgbox(0, '' , "no matches for '" & $sMatch & "'")

return $aOut

EndFunc
Edited by boththose

,-. .--. ________ .-. .-. ,---. ,-. .-. .-. .-.
|(| / /\ \ |\ /| |__ __||| | | || .-' | |/ / \ \_/ )/
(_) / /__\ \ |(\ / | )| | | `-' | | `-. | | / __ \ (_)
| | | __ | (_)\/ | (_) | | .-. | | .-' | | \ |__| ) (
| | | | |)| | \ / | | | | | |)| | `--. | |) \ | |
`-' |_| (_) | |\/| | `-' /( (_)/( __.' |((_)-' /(_|
'-' '-' (__) (__) (_) (__)

Share this post


Link to post
Share on other sites

Seems like I was trying to make things too difficult for myself. Thank you so much, boththose! 

Is there an easy way to display the results in the ListView of my GUI?

Thanks again.

Share this post


Link to post
Share on other sites

#4 ·  Posted (edited)

Is there an easy way to display the results in the ListView of my GUI?

 

Can you can post your GUI and LV?    I imagine these functions will help though  

 _GUICtrlListView_AddArray

 

_GUICtrlListView_AddItem

 

 

Edited by boththose

,-. .--. ________ .-. .-. ,---. ,-. .-. .-. .-.
|(| / /\ \ |\ /| |__ __||| | | || .-' | |/ / \ \_/ )/
(_) / /__\ \ |(\ / | )| | | `-' | | `-. | | / __ \ (_)
| | | __ | (_)\/ | (_) | | .-. | | .-' | | \ |__| ) (
| | | | |)| | \ / | | | | | |)| | `--. | |) \ | |
`-' |_| (_) | |\/| | `-' /( (_)/( __.' |((_)-' /(_|
'-' '-' (__) (__) (_) (__)

Share this post


Link to post
Share on other sites

#5 ·  Posted (edited)

I'm afraid to post my GUI here because I know it's probably one of the worst codes ever written, but here goes nothing:

#include <Array.au3>
#include <File.au3>
#include <ButtonConstants.au3>
#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>
#include <ListviewConstants.au3>
#include <GuiListView.au3>
#include "_FindInFile.au3"
#include "_XMLDomWrapper.au3"


Hovedmeny()

Func Hovedmeny()

;==> Hovedmeny
    Global $Hovedmeny = GUICreate("Hovedmeny", 623, 449, -1, -1)
    Global $Butikkdata = GUICtrlCreateButton("Butikkdata", 64, 160, 100, 100)
    Global $Varebestand = GUICtrlCreateButton("Varebestand", 184, 160, 100, 100)
    Global $Statistikk = GUICtrlCreateButton("Statistikk", 304, 160, 100, 100)
    Global $Importer = GUICtrlCreateButton("Importer", 424, 160, 100, 100)
    Global $MBBYGG = GUICtrlCreateLabel("Mjømen og Berge Byggverksemd AS", 144, 24, 316, 28)
    GUICtrlSetFont(-1, 14, 400, 0, "MS Sans Serif")
    Global $Copyright = GUICtrlCreateLabel("Copyright© 2015  Mathias Søvik Mjømen – All Rights Reserved", 0, 431, 622, 17)
    GUISetState(@SW_SHOW)
;==> /Hovedmeny

;==> Butikkdata
    Global $Varer = GUICtrlCreateButton("Varer", 64, 160, 100, 100)
    Global $ImporterFraScanner = GUICtrlCreateButton("Scanner", 184, 160, 100, 100)
    Global $ImporterFraOrdre = GUICtrlCreateButton("Ordre", 304, 160, 100, 100)
    Global $ImporterFraTilbud = GUICtrlCreateButton("Tilbud", 424, 160, 100, 100)

    GUICtrlSetState($Varer, 32)
    GUICtrlSetState($ImporterFraScanner, 32)
    GUICtrlSetState($ImporterFraOrdre, 32)
    GUICtrlSetState($ImporterFraTilbud, 32)
;==> /Butikkdata




;==> Varer
    Global $Treverk = GUICtrlCreateButton("Treverk", 80, 304, 60, 60)
    Global $Festemidler = GUICtrlCreateButton("Festemidler", 144, 304, 60, 60)
    Global $Utlan = GUICtrlCreateButton("Utlån", 208, 304, 60, 60)
    Global $Stein = GUICtrlCreateButton("Stein", 272, 304, 60, 60)
    Global $Bat = GUICtrlCreateButton("Båt", 336, 304, 60, 60)
    Global $Maling = GUICtrlCreateButton("Maling", 400, 304, 60, 60)
    Global $Diverse = GUICtrlCreateButton("Diverse", 464, 304, 60, 60)
    Global $Input = GUICtrlCreateInput("", 104, 144, 401, 21)
    Global $Sok = GUICtrlCreateButton("Søk", 264, 192, 75, 25)

    GUICtrlSetState($Treverk, 32)
    GUICtrlSetState($Festemidler, 32)
    GUICtrlSetState($Utlan, 32)
    GUICtrlSetState($Stein, 32)
    GUICtrlSetState($Bat, 32)
    GUICtrlSetState($Maling, 32)
    GUICtrlSetState($Diverse, 32)
    GUICtrlSetState($Input, 32)
    GUICtrlSetState($Sok, 32)
;==> /Varer

;==> Søk


    Global $SearchView = GUICtrlCreateListView("Søk", 104, 144, 401, 200)
    _GUICtrlListView_SetColumnWidth($SearchView, 0, 400 )


    GUICtrlSetState($SearchView, 32)
;==> /Søk

    While 1
        $nMsg = GUIGetMsg()
        Switch $nMsg
            Case $GUI_EVENT_CLOSE
                ExitLoop


            Case $Butikkdata
                Butikkdata()

        EndSwitch
    WEnd

EndFunc   ;==>Hovedmeny

Func Butikkdata()
    GUICtrlSetState($Butikkdata, 32)
    GUICtrlSetState($Varebestand, 32)
    GUICtrlSetState($Statistikk, 32)
    GUICtrlSetState($Importer, 32)
    GUICtrlSetState($Varer, 16)
    GUICtrlSetState($ImporterFraScanner, 16)
    GUICtrlSetState($ImporterFraOrdre, 16)
    GUICtrlSetState($ImporterFraTilbud, 16)
    While 1
        $nMsg = GUIGetMsg()
        Switch $nMsg
            Case $GUI_EVENT_CLOSE
                Exit

            Case $Varer
                Varer()


        EndSwitch
    WEnd

EndFunc   ;==>Butikkdata

Func Varer()
    GUICtrlSetState($Varer, 32)
    GUICtrlSetState($ImporterFraScanner, 32)
    GUICtrlSetState($ImporterFraOrdre, 32)
    GUICtrlSetState($ImporterFraTilbud, 32)

    GUICtrlSetState($Treverk, 16)
    GUICtrlSetState($Festemidler, 16)
    GUICtrlSetState($Utlan, 16)
    GUICtrlSetState($Stein, 16)
    GUICtrlSetState($Bat, 16)
    GUICtrlSetState($Maling, 16)
    GUICtrlSetState($Diverse, 16)
    GUICtrlSetState($Input, 16)
    GUICtrlSetState($Sok, 16)

    While 1
        $nMsg = GUIGetMsg()
        Switch $nMsg
            Case $GUI_EVENT_CLOSE
                Exit

            Case $Sok
                Sok()

                $Input2 = GUICtrlRead($Input)
                _ArrayDisplay(_2LinesBeforeAndAfter("C:\Users\Mathias\Dropbox\Mjømen og Berge\MBBYGG\Development\XML\Varer.xml" , $Input2)) ; 1 return



        EndSwitch
    WEnd

EndFunc   ;==>Varer


Func Sok()
    GUICtrlSetState($Treverk, 32)
    GUICtrlSetState($Festemidler, 32)
    GUICtrlSetState($Utlan, 32)
    GUICtrlSetState($Stein, 32)
    GUICtrlSetState($Bat, 32)
    GUICtrlSetState($Maling, 32)
    GUICtrlSetState($Diverse, 32)
    GUICtrlSetState($Input, 32)
    GUICtrlSetState($Sok, 32)

    GUICtrlSetState($SearchView, 16)





EndFunc

Func _2LinesBeforeAndAfter($File , $sMatch)

Local $aArray
Local $aOut[0]
_FileReadToArray($File , $aArray)

For $i = 0 to ubound($aArray) - 1
    If stringinstr($aArray[$i] , $sMatch) Then
        _ArrayAdd($aOut , $aArray[$i - 2])
        _ArrayAdd($aOut , $aArray[$i - 1])
        _ArrayAdd($aOut , $aArray[$i])
        _ArrayAdd($aOut , $aArray[$i + 1])
        _ArrayAdd($aOut , $aArray[$i + 2])
        _ArrayAdd($aOut , "")
    EndIf
Next


If ubound($aOut) = 0 then $aOut = msgbox(0, '' , "no matches for '" & $sMatch & "'")

return $aOut

EndFunc
Edited by despertar

Share this post


Link to post
Share on other sites

#include <Array.au3>
#include <File.au3>
#include <ButtonConstants.au3>
#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>
#include <ListviewConstants.au3>
#include <GuiListView.au3>

Hovedmeny()

Func Hovedmeny()

;==> Hovedmeny
    Global $Hovedmeny = GUICreate("Hovedmeny", 623, 449, -1, -1)
    Global $Butikkdata = GUICtrlCreateButton("Butikkdata", 64, 160, 100, 100)
    Global $Varebestand = GUICtrlCreateButton("Varebestand", 184, 160, 100, 100)
    Global $Statistikk = GUICtrlCreateButton("Statistikk", 304, 160, 100, 100)
    Global $Importer = GUICtrlCreateButton("Importer", 424, 160, 100, 100)
    Global $MBBYGG = GUICtrlCreateLabel("Mjømen og Berge Byggverksemd AS", 144, 24, 316, 28)
    GUICtrlSetFont(-1, 14, 400, 0, "MS Sans Serif")
    Global $Copyright = GUICtrlCreateLabel("Copyright© 2015  Mathias Søvik Mjømen – All Rights Reserved", 0, 431, 622, 17)
    GUISetState(@SW_SHOW)
;==> /Hovedmeny

;==> Butikkdata
    Global $Varer = GUICtrlCreateButton("Varer", 64, 160, 100, 100)
    Global $ImporterFraScanner = GUICtrlCreateButton("Scanner", 184, 160, 100, 100)
    Global $ImporterFraOrdre = GUICtrlCreateButton("Ordre", 304, 160, 100, 100)
    Global $ImporterFraTilbud = GUICtrlCreateButton("Tilbud", 424, 160, 100, 100)

    GUICtrlSetState($Varer, 32)
    GUICtrlSetState($ImporterFraScanner, 32)
    GUICtrlSetState($ImporterFraOrdre, 32)
    GUICtrlSetState($ImporterFraTilbud, 32)
;==> /Butikkdata




;==> Varer
    Global $Treverk = GUICtrlCreateButton("Treverk", 80, 304, 60, 60)
    Global $Festemidler = GUICtrlCreateButton("Festemidler", 144, 304, 60, 60)
    Global $Utlan = GUICtrlCreateButton("Utlån", 208, 304, 60, 60)
    Global $Stein = GUICtrlCreateButton("Stein", 272, 304, 60, 60)
    Global $Bat = GUICtrlCreateButton("Båt", 336, 304, 60, 60)
    Global $Maling = GUICtrlCreateButton("Maling", 400, 304, 60, 60)
    Global $Diverse = GUICtrlCreateButton("Diverse", 464, 304, 60, 60)
    Global $Input = GUICtrlCreateInput("", 104, 144, 401, 21)
    Global $Sok = GUICtrlCreateButton("Søk", 264, 192, 75, 25)

    GUICtrlSetState($Treverk, 32)
    GUICtrlSetState($Festemidler, 32)
    GUICtrlSetState($Utlan, 32)
    GUICtrlSetState($Stein, 32)
    GUICtrlSetState($Bat, 32)
    GUICtrlSetState($Maling, 32)
    GUICtrlSetState($Diverse, 32)
    GUICtrlSetState($Input, 32)
    GUICtrlSetState($Sok, 32)
;==> /Varer

;==> Søk


    Global $SearchView = GUICtrlCreateListView("Søk", 104, 144, 401, 200)
    _GUICtrlListView_SetColumnWidth($SearchView, 0, 400 )


    GUICtrlSetState($SearchView, 32)
;==> /Søk

    While 1
        $nMsg = GUIGetMsg()
        Switch $nMsg
            Case $GUI_EVENT_CLOSE
                ExitLoop


            Case $Butikkdata
                Butikkdata()

        EndSwitch
    WEnd

EndFunc   ;==>Hovedmeny

Func Butikkdata()
    GUICtrlSetState($Butikkdata, 32)
    GUICtrlSetState($Varebestand, 32)
    GUICtrlSetState($Statistikk, 32)
    GUICtrlSetState($Importer, 32)
    GUICtrlSetState($Varer, 16)
    GUICtrlSetState($ImporterFraScanner, 16)
    GUICtrlSetState($ImporterFraOrdre, 16)
    GUICtrlSetState($ImporterFraTilbud, 16)
    While 1
        $nMsg = GUIGetMsg()
        Switch $nMsg
            Case $GUI_EVENT_CLOSE
                Exit

            Case $Varer
                Varer()


        EndSwitch
    WEnd

EndFunc   ;==>Butikkdata

Func Varer()
    GUICtrlSetState($Varer, 32)
    GUICtrlSetState($ImporterFraScanner, 32)
    GUICtrlSetState($ImporterFraOrdre, 32)
    GUICtrlSetState($ImporterFraTilbud, 32)

    GUICtrlSetState($Treverk, 16)
    GUICtrlSetState($Festemidler, 16)
    GUICtrlSetState($Utlan, 16)
    GUICtrlSetState($Stein, 16)
    GUICtrlSetState($Bat, 16)
    GUICtrlSetState($Maling, 16)
    GUICtrlSetState($Diverse, 16)
    GUICtrlSetState($Input, 16)
    GUICtrlSetState($Sok, 16)

    While 1
        $nMsg = GUIGetMsg()
        Switch $nMsg
            Case $GUI_EVENT_CLOSE
                Exit

            Case $Sok
                Sok()

                $Input2 = GUICtrlRead($Input)
                _ArrayDisplay(_2LinesBeforeAndAfter("Varer.xml" , $Input2)) ; 1 return



        EndSwitch
    WEnd

EndFunc   ;==>Varer


Func Sok()
    GUICtrlSetState($Treverk, 32)
    GUICtrlSetState($Festemidler, 32)
    GUICtrlSetState($Utlan, 32)
    GUICtrlSetState($Stein, 32)
    GUICtrlSetState($Bat, 32)
    GUICtrlSetState($Maling, 32)
    GUICtrlSetState($Diverse, 32)
    GUICtrlSetState($Input, 32)
    GUICtrlSetState($Sok, 32)

    GUICtrlSetState($SearchView, 16)





EndFunc

Func _2LinesBeforeAndAfter($File , $sMatch)

Local $aArray
Local $count = 0
_FileReadToArray($File , $aArray)

For $i = 0 to ubound($aArray) - 1
    If stringinstr($aArray[$i] , $sMatch) Then

_GUICtrlListView_AddItem($SearchView , stringtrimright(stringtrimleft(stringstripws($aArray[$i - 2] , 1) , 10) , 11))
_GUICtrlListView_AddItem($SearchView , stringtrimright(stringtrimleft(stringstripws($aArray[$i - 1] , 1) , 6) , 7))
_GUICtrlListView_AddItem($SearchView , stringtrimright(stringtrimleft(stringstripws($aArray[$i] , 1) , 13) , 14))
_GUICtrlListView_AddItem($SearchView , stringtrimright(stringtrimleft(stringstripws($aArray[$i + 1] , 1) , 10) , 11))
_GUICtrlListView_AddItem($SearchView , stringtrimright(stringtrimleft(stringstripws($aArray[$i + 2] , 1) , 5) , 6))
_GUICtrlListView_AddItem($SearchView , "")
$count = 1
    EndIf
Next

If $count = 0 then msgbox(0, '' , "no matches for '" & $sMatch & "'")

EndFunc


,-. .--. ________ .-. .-. ,---. ,-. .-. .-. .-.
|(| / /\ \ |\ /| |__ __||| | | || .-' | |/ / \ \_/ )/
(_) / /__\ \ |(\ / | )| | | `-' | | `-. | | / __ \ (_)
| | | __ | (_)\/ | (_) | | .-. | | .-' | | \ |__| ) (
| | | | |)| | \ / | | | | | |)| | `--. | |) \ | |
`-' |_| (_) | |\/| | `-' /( (_)/( __.' |((_)-' /(_|
'-' '-' (__) (__) (_) (__)

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now