Jump to content

Recommended Posts

Posted (edited)

Hey guys, I need a little help with my SRE pattern.

I'm attempting to extract just a few attributes from an XML file. I thought I could get this to work, but I'm hitting an unforeseen dilemma here.

I have included a reproducer script containing 2 example objects and the differences between possible attributes contained in separate objects, along with my attempt at an SRE pattern. I don't need to collect the values of all attributes, just a select few attributes if the object contains them.

#include <array.au3>

Local $sText = "" & _
                "<Item CatalogID='ID_15674'>" & _
                "        <SUPPLIER_NAME_STRING id='ID1014_161'/>" & _
                "        <BRANDNAME_STRING id='ID1043_111'/>" & _
                "        <QRYTEXT_STRING id='ID1082_8519'/>" & _
                "        <RARITY_STATUS id='ID1164_2'/>" & _
                "        <ARTID value='27700'/>" & _
                "        <SUPPLIER_NUMBER value='127700'/>" & _
                "        <OFFSET_ID value='ID_22265'/>" & _
                "        <STAMP value='256/350'/>" & _
                "        <STYLE value='1'/>" & _
                "        <IS_PROMOTIONAL value='0'/>" & _
                "</Item>" & _
                "<Item CatalogID='ID_15675'>" & _
                "        <SUPPLIER_NUMBER value='389844'/>" & _
                "        <OFFSET_ID value='ID_15674'/>" & _
                "        <IS_COMPLETED/> " & _
                "</Item>"

Local $srePattern = "(?mx) " & _
                    "^ \<Item CatalogID\=\'(.*?)\'\>" & _
                    "(\<BRANDNAME\_STRING id\=\'(.*?)\'\/\>)?" & _
                    "(\<COLLECTION\_STATUS id\=\'(.*?)\'\/\>)?" & _
                    "(\<OFFSET\_ID value\=\'(.*?)\'\/\>)?" & _
                    "(\<STAMP value\=\'(.*?)\'\/\>)?" & _
                    "(\<IS\_PROMOTIONAL value\=\'(.*?)\'\/\>)?" & _
                    "(\<IS\_COMPLETED\/\>)?" & _
                    "(\<\/Item\>$"


$aSRE = StringRegExp( $sText, $srePattern,3)
If @error Then MsgBox('','SRE Error',@error & @CRLF & StringLeft($srePattern,@extended))

_ArrayDisplay($aSRE)

 

Edited by Realm

My Contributions: Unix Timestamp: Calculate Unix time, or seconds since Epoch, accounting for your local timezone and daylight savings time. RegEdit Jumper: A Small & Simple interface based on Yashied's Reg Jumper Function, for searching Hives in your registry. 

Posted (edited)

in your pattern you have an error here:

; "(\<\/Item\>$"
"(\<\/Item\>)"

and at the begining:

Local $srePattern = "(?is)" & _
                    "<Item

and finally added :

Local $srePattern = "(?is)" & _
                    "<Item CatalogID\=\'(.*?)\'\>.*?" & _
                    "(\<BRANDNAME\_STRING id\=\'(.*?)\'\/\>)?.*?" & _
                    "(\<COLLECTION\_STATUS id\=\'(.*?)\'\/\>)?.*?" & _
                    "(\<OFFSET\_ID value\=\'(.*?)\'\/\>)?.*?" & _
                    "(\<STAMP value\=\'(.*?)\'\/\>)?.*?" & _
                    "(\<IS\_PROMOTIONAL value\=\'(.*?)\'\/\>)?.*?" & _
                    "(\<IS\_COMPLETED\/\>)?.*?" & _
                    "(\<\/Item\>)"

How you can se I add some .*?

But I think this is not what you need ....

btw.
Why you do not use XML DOM ? or XMLWrapper UDF ?

 

Edited by mLipok

Signature beginning:
Please remember: "AutoIt"..... *  Wondering who uses AutoIt and what it can be used for ? * Forum Rules *
ADO.au3 UDF * POP3.au3 UDF * XML.au3 UDF * IE on Windows 11 * How to ask ChatGPT for AutoIt Codefor other useful stuff click the following button:

  Reveal hidden contents

Signature last update: 2023-04-24

Posted (edited)
  On 5/16/2016 at 6:21 AM, mLipok said:

Why you do not use XML DOM ? or XMLWrapper UDF ?

Expand  

I have absolutely no experience with XML files.

I have tried using the XMLWrapper long ago, but wasn't too pleased with it's speed and found it to be a little too complicated and difficult to understand.

However I did try to reproduce an example that you written to help another forum member from this post:

 

I wasn't able to read the child nodes or extract the attributes I'm looking for. So I turned to SRE for a quick and dirty approach.

I am sure DOM would be more reliable, but would it be a faster, or as fast, approach?

Edited by Realm

My Contributions: Unix Timestamp: Calculate Unix time, or seconds since Epoch, accounting for your local timezone and daylight savings time. RegEdit Jumper: A Small & Simple interface based on Yashied's Reg Jumper Function, for searching Hives in your registry. 

Posted

Could you try this:

#AutoIt3Wrapper_Au3Check_Parameters=-d -w 1 -w 2 -w 3 -w- 4 -w 5 -w 6 -w 7
#Tidy_Parameters=/sort_funcs /reel

#include <array.au3>
#include <date.au3>
#include <MsgBoxConstants.au3>
#include "XML.au3"

; This COM Error Hanlder will be used globally (excepting inside UDF Functions)
Global $oErrorHandler = ObjEvent("AutoIt.Error", ErrFunc_CustomUserHandler_MAIN)
#forceref $oErrorHandler

; This is SetUp for the transfer UDF internal COM Error Handler to the user function
_XML_ComErrorHandler_UserFunction(ErrFunc_CustomUserHandler_XML)

Example_1__XML_SelectNodes()

#Region XML__Examples.au3 - Function

Func Example_1__XML_SelectNodes()

    ; first you must create $oXmlDoc object
    Local $oXMLDoc = _XML_CreateDOMDocument(Default)
    If @error Then
        MsgBox(0, '_XML_CreateDOMDocument @error:', XML_My_ErrorParser(@error))
    Else
        ; Load file to $oXmlDoc
        Local $sXML_Content = "" & _
                "<Checkin>" & _
                "<Item CatalogID='ID_15674'>" & _
                "        <SUPPLIER_NAME_STRING id='ID1014_161'/>" & _
                "        <BRANDNAME_STRING id='ID1043_111'/>" & _
                "        <QRYTEXT_STRING id='ID1082_8519'/>" & _
                "        <RARITY_STATUS id='ID1164_2'/>" & _
                "        <ARTID value='27700'/>" & _
                "        <SUPPLIER_NUMBER value='127700'/>" & _
                "        <OFFSET_ID value='ID_22265'/>" & _
                "        <STAMP value='256/350'/>" & _
                "        <STYLE value='1'/>" & _
                "        <IS_PROMOTIONAL value='0'/>" & _
                "</Item>" & _
                "<Item CatalogID='ID_15675'>" & _
                "        <SUPPLIER_NUMBER value='389844'/>" & _
                "        <OFFSET_ID value='ID_15674'/>" & _
                "        <IS_COMPLETED/> " & _
                "</Item>" & _
                "</Checkin>" & _
                ""
        _XML_LoadXml($oXMLDoc, $sXML_Content)
        If @error Then
            MsgBox(0, '_XML_Load @error:', XML_My_ErrorParser(@error))
            MsgBox(0, '', _XML_ErrorParser_GetDescription($oXMLDoc))
        Else
            ; simple display $oXmlDoc - for checking only
            Local $sXmlAfterTidy = _XML_TIDY($oXMLDoc)
            If @error Then
                MsgBox(0, '_XML_TIDY @error:', XML_My_ErrorParser(@error))
            Else
                MsgBox($MB_SYSTEMMODAL + $MB_ICONINFORMATION, 'Example_1__XML_SelectNodes', $sXmlAfterTidy)
            EndIf

            ; selecting nodes
            _XML_SelectNodes($oXMLDoc, "//Item")
            If @error Then
                MsgBox(0, '_XML_SelectNodes @error:', XML_My_ErrorParser(@error))
                MsgBox(0, '', _XML_ErrorParser_GetDescription($oXMLDoc))
            Else
                Local $iNodesCount = @extended
                MsgBox(0, '$iNodesCount ', $iNodesCount)
                For $iItem_idx = 1 To $iNodesCount
                    Local $oChilds_Coll = _XML_SelectNodes($oXMLDoc, '//Checkin/Item[' & $iItem_idx & ']/*')
                    Local $aNodesColl = _XML_Array_GetNodesProperties($oChilds_Coll)
                    If @error Then
                        MsgBox(0, '_XML_Array_GetNodesProperties($oNodesColl)', XML_My_ErrorParser(@error))
                    Else
                        ; display array
                        _ArrayDisplay($aNodesColl, 'Example_1__XML_SelectNodes ' & $iItem_idx)
                    EndIf
                Next

            EndIf
        EndIf
    EndIf
EndFunc   ;==>Example_1__XML_SelectNodes
#EndRegion XML__Examples.au3 - Function

#Region XML__Examples.au3 - XML DOM Error/Event Handling

Func ErrFunc_CustomUserHandler_MAIN($oError)

    ConsoleWrite(@ScriptName & " (" & $oError.scriptline & ") : MainScript ==> COM Error intercepted !" & @CRLF & _
            @TAB & "err.number is: " & @TAB & @TAB & "0x" & Hex($oError.number) & @CRLF & _
            @TAB & "err.windescription:" & @TAB & $oError.windescription & @CRLF & _
            @TAB & "err.description is: " & @TAB & $oError.description & @CRLF & _
            @TAB & "err.source is: " & @TAB & @TAB & $oError.source & @CRLF & _
            @TAB & "err.helpfile is: " & @TAB & $oError.helpfile & @CRLF & _
            @TAB & "err.helpcontext is: " & @TAB & $oError.helpcontext & @CRLF & _
            @TAB & "err.lastdllerror is: " & @TAB & $oError.lastdllerror & @CRLF & _
            @TAB & "err.scriptline is: " & @TAB & $oError.scriptline & @CRLF & _
            @TAB & "err.retcode is: " & @TAB & "0x" & Hex($oError.retcode) & @CRLF & @CRLF)
EndFunc   ;==>ErrFunc_CustomUserHandler_MAIN

Func ErrFunc_CustomUserHandler_XML($oError)

    ; here is declared another path to UDF au3 file
    ; thanks to this with using _XML_ComErrorHandler_UserFunction(ErrFunc_CustomUserHandler_XML)
    ;  you get errors which after pressing F4 in SciTE4AutoIt you goes directly to the specified UDF Error Line
    ConsoleWrite(@ScriptDir & '\XML.au3' & " (" & $oError.scriptline & ") : UDF ==> COM Error intercepted ! " & @CRLF & _
            @TAB & "err.number is: " & @TAB & @TAB & "0x" & Hex($oError.number) & @CRLF & _
            @TAB & "err.windescription:" & @TAB & $oError.windescription & @CRLF & _
            @TAB & "err.description is: " & @TAB & $oError.description & @CRLF & _
            @TAB & "err.source is: " & @TAB & @TAB & $oError.source & @CRLF & _
            @TAB & "err.helpfile is: " & @TAB & $oError.helpfile & @CRLF & _
            @TAB & "err.helpcontext is: " & @TAB & $oError.helpcontext & @CRLF & _
            @TAB & "err.lastdllerror is: " & @TAB & $oError.lastdllerror & @CRLF & _
            @TAB & "err.scriptline is: " & @TAB & $oError.scriptline & @CRLF & _
            @TAB & "err.retcode is: " & @TAB & "0x" & Hex($oError.retcode) & @CRLF & @CRLF)
EndFunc   ;==>ErrFunc_CustomUserHandler_XML

; #FUNCTION# ====================================================================================================================
; Name ..........: XML_My_ErrorParser
; Description ...: Changing $XML_ERR_ ... to human readable description
; Syntax ........: XML_My_ErrorParser($iXMLWrapper_Error, $iXMLWrapper_Extended)
; Parameters ....: $iXMLWrapper_Error   - an integer value.
;                  $iXMLWrapper_Extended           - an integer value.
; Return values .: description as string
; Author ........: mLipok
; Modified ......:
; Remarks .......: This function is only example of how user can parse @error and @extended to human readable description
; Related .......:
; Link ..........:
; Example .......: No
; ===============================================================================================================================
Func XML_My_ErrorParser($iXMLWrapper_Error, $iXMLWrapper_Extended = 0)
    Local $sErrorInfo = ''
    Switch $iXMLWrapper_Error
        Case $XML_ERR_OK
            $sErrorInfo = '$XML_ERR_OK=' & $XML_ERR_OK & @CRLF & 'All is ok.'
        Case $XML_ERR_GENERAL
            $sErrorInfo = '$XML_ERR_GENERAL=' & $XML_ERR_GENERAL & @CRLF & 'The error which is not specifically defined.'
        Case $XML_ERR_COMERROR
            $sErrorInfo = '$XML_ERR_COMERROR=' & $XML_ERR_COMERROR & @CRLF & 'COM ERROR OCCURED. Check @extended and your own error handler function for details.'
        Case $XML_ERR_ISNOTOBJECT
            $sErrorInfo = '$XML_ERR_ISNOTOBJECT=' & $XML_ERR_ISNOTOBJECT & @CRLF & 'No object passed to function'
        Case $XML_ERR_INVALIDDOMDOC
            $sErrorInfo = '$XML_ERR_INVALIDDOMDOC=' & $XML_ERR_INVALIDDOMDOC & @CRLF & 'Invalid object passed to function'
        Case $XML_ERR_INVALIDATTRIB
            $sErrorInfo = '$XML_ERR_INVALIDATTRIB=' & $XML_ERR_INVALIDATTRIB & @CRLF & 'Invalid object passed to function.'
        Case $XML_ERR_INVALIDNODETYPE
            $sErrorInfo = '$XML_ERR_INVALIDNODETYPE=' & $XML_ERR_INVALIDNODETYPE & @CRLF & 'Invalid object passed to function.'
        Case $XML_ERR_OBJCREATE
            $sErrorInfo = '$XML_ERR_OBJCREATE=' & $XML_ERR_OBJCREATE & @CRLF & 'Object can not be created.'
        Case $XML_ERR_NODECREATE
            $sErrorInfo = '$XML_ERR_NODECREATE=' & $XML_ERR_NODECREATE & @CRLF & 'Can not create Node - check also COM Error Handler'
        Case $XML_ERR_NODEAPPEND
            $sErrorInfo = '$XML_ERR_NODEAPPEND=' & $XML_ERR_NODEAPPEND & @CRLF & 'Can not append Node - check also COM Error Handler'
        Case $XML_ERR_PARSE
            $sErrorInfo = '$XML_ERR_PARSE=' & $XML_ERR_PARSE & @CRLF & 'Error: with Parsing objects, .parseError.errorCode=' & $iXMLWrapper_Extended & ' Use _XML_ErrorParser_GetDescription() for get details.'
        Case $XML_ERR_PARSE_XSL
            $sErrorInfo = '$XML_ERR_PARSE_XSL=' & $XML_ERR_PARSE_XSL & @CRLF & 'Error with Parsing XSL objects .parseError.errorCode=' & $iXMLWrapper_Extended & ' Use _XML_ErrorParser_GetDescription() for get details.'
        Case $XML_ERR_LOAD
            $sErrorInfo = '$XML_ERR_LOAD=' & $XML_ERR_LOAD & @CRLF & 'Error opening specified file.'
        Case $XML_ERR_SAVE
            $sErrorInfo = '$XML_ERR_SAVE=' & $XML_ERR_SAVE & @CRLF & 'Error saving file.'
        Case $XML_ERR_PARAMETER
            $sErrorInfo = '$XML_ERR_PARAMETER=' & $XML_ERR_PARAMETER & @CRLF & 'Wrong parameter passed to function.'
        Case $XML_ERR_ARRAY
            $sErrorInfo = '$XML_ERR_ARRAY=' & $XML_ERR_ARRAY & @CRLF & 'Wrong array parameter passed to function. Check array dimension and conent.'
        Case $XML_ERR_XPATH
            $sErrorInfo = '$XML_ERR_XPATH=' & $XML_ERR_XPATH & @CRLF & 'XPath syntax error - check also COM Error Handler.'
        Case $XML_ERR_NONODESMATCH
            $sErrorInfo = '$XML_ERR_NONODESMATCH=' & $XML_ERR_NONODESMATCH & @CRLF & 'No nodes match the XPath expression'
        Case $XML_ERR_NOCHILDMATCH
            $sErrorInfo = '$XML_ERR_NOCHILDMATCH=' & $XML_ERR_NOCHILDMATCH & @CRLF & 'There is no Child in nodes matched by XPath expression.'
        Case $XML_ERR_NOATTRMATCH
            $sErrorInfo = '$XML_ERR_NOATTRMATCH=' & $XML_ERR_NOATTRMATCH & @CRLF & 'There is no such attribute in selected node.'
        Case $XML_ERR_DOMVERSION
            $sErrorInfo = '$XML_ERR_DOMVERSION=' & $XML_ERR_DOMVERSION & @CRLF & 'DOM Version: ' & 'MSXML Version ' & $iXMLWrapper_Extended & ' or greater required for this function'
        Case $XML_ERR_EMPTYCOLLECTION
            $sErrorInfo = '$XML_ERR_EMPTYCOLLECTION=' & $XML_ERR_EMPTYCOLLECTION & @CRLF & 'Collections of objects was empty'
        Case $XML_ERR_EMPTYOBJECT
            $sErrorInfo = '$XML_ERR_EMPTYOBJECT=' & $XML_ERR_EMPTYOBJECT & @CRLF & 'Object is empty'
        Case Else
            $sErrorInfo = '=' & $iXMLWrapper_Error & @CRLF & 'NO ERROR DESCRIPTION FOR THIS @error'
    EndSwitch

    Local $sExtendedInfo = ''
    Switch $iXMLWrapper_Error
        Case $XML_ERR_COMERROR, $XML_ERR_NODEAPPEND, $XML_ERR_NODECREATE
            $sExtendedInfo = 'COM ERROR NUMBER (@error returned via @extended) =' & $iXMLWrapper_Extended
        Case $XML_ERR_PARAMETER
            $sExtendedInfo = 'This @error was fired by parameter: #' & $iXMLWrapper_Extended
        Case Else
            Switch $iXMLWrapper_Extended
                Case $XML_EXT_DEFAULT
                    $sExtendedInfo = '$XML_EXT_DEFAULT=' & $XML_EXT_DEFAULT & @CRLF & 'Default - Do not return any additional information'
                Case $XML_EXT_XMLDOM
                    $sExtendedInfo = '$XML_EXT_XMLDOM=' & $XML_EXT_XMLDOM & @CRLF & '"Microsoft.XMLDOM" related Error'
                Case $XML_EXT_DOMDOCUMENT
                    $sExtendedInfo = '$XML_EXT_DOMDOCUMENT=' & $XML_EXT_DOMDOCUMENT & @CRLF & '"Msxml2.DOMDocument" related Error'
                Case $XML_EXT_XSLTEMPLATE
                    $sExtendedInfo = '$XML_EXT_XSLTEMPLATE=' & $XML_EXT_XSLTEMPLATE & @CRLF & '"Msxml2.XSLTemplate" related Error'
                Case $XML_EXT_SAXXMLREADER
                    $sExtendedInfo = '$XML_EXT_SAXXMLREADER=' & $XML_EXT_SAXXMLREADER & @CRLF & '"MSXML2.SAXXMLReader" related Error'
                Case $XML_EXT_MXXMLWRITER
                    $sExtendedInfo = '$XML_EXT_MXXMLWRITER=' & $XML_EXT_MXXMLWRITER & @CRLF & '"MSXML2.MXXMLWriter" related Error'
                Case $XML_EXT_FREETHREADEDDOMDOCUMENT
                    $sExtendedInfo = '$XML_EXT_FREETHREADEDDOMDOCUMENT=' & $XML_EXT_FREETHREADEDDOMDOCUMENT & @CRLF & '"Msxml2.FreeThreadedDOMDocument" related Error'
                Case $XML_EXT_XMLSCHEMACACHE
                    $sExtendedInfo = '$XML_EXT_XMLSCHEMACACHE=' & $XML_EXT_XMLSCHEMACACHE & @CRLF & '"Msxml2.XMLSchemaCache." related Error'
                Case $XML_EXT_STREAM
                    $sExtendedInfo = '$XML_EXT_STREAM=' & $XML_EXT_STREAM & @CRLF & '"ADODB.STREAM" related Error'
                Case $XML_EXT_ENCODING
                    $sExtendedInfo = '$XML_EXT_ENCODING=' & $XML_EXT_ENCODING & @CRLF & 'Encoding related Error'
                Case Else
                    $sExtendedInfo = '$iXMLWrapper_Extended=' & $iXMLWrapper_Extended & @CRLF & 'NO ERROR DESCRIPTION FOR THIS @extened'
            EndSwitch
    EndSwitch
    ; return back @error and @extended for further debuging
    Return SetError($iXMLWrapper_Error, $iXMLWrapper_Extended, _
            '@error description:' & @CRLF & _
            $sErrorInfo & @CRLF & _
            @CRLF & _
            '@extended description:' & @CRLF & _
            $sExtendedInfo & @CRLF & _
            '')

EndFunc   ;==>XML_My_ErrorParser
#EndRegion XML__Examples.au3 - XML DOM Error/Event Handling

 

Signature beginning:
Please remember: "AutoIt"..... *  Wondering who uses AutoIt and what it can be used for ? * Forum Rules *
ADO.au3 UDF * POP3.au3 UDF * XML.au3 UDF * IE on Windows 11 * How to ask ChatGPT for AutoIt Codefor other useful stuff click the following button:

  Reveal hidden contents

Signature last update: 2023-04-24

Posted
  On 5/18/2016 at 9:29 PM, mLipok said:

Could you try this:

 

Expand  

This works beautifully on the actual files. Thanks.

I looked at your UDF Example in the forums and noticed that you took it upon yourself to rewrite the original wrapper. I do believe it was the old wrapper I tried using a year or so ago and found difficult to understand and use. I'm going to take a closer look at your UDF and see what I can learn and do with it. I do believe this will be a faster and more reliable approach. Thanks for replying and all your hard work on the wrapper!

My Contributions: Unix Timestamp: Calculate Unix time, or seconds since Epoch, accounting for your local timezone and daylight savings time. RegEdit Jumper: A Small & Simple interface based on Yashied's Reg Jumper Function, for searching Hives in your registry. 

Posted
  On 5/21/2016 at 11:10 PM, Realm said:

Thanks for replying and all your hard work on the wrapper!

Expand  

I'm tying to learn something about XML .... and other technologies.
Only solving diferent problems there is a possibility to making progress.

For that I publish my work and trying to improve each of my UDF.

Signature beginning:
Please remember: "AutoIt"..... *  Wondering who uses AutoIt and what it can be used for ? * Forum Rules *
ADO.au3 UDF * POP3.au3 UDF * XML.au3 UDF * IE on Windows 11 * How to ask ChatGPT for AutoIt Codefor other useful stuff click the following button:

  Reveal hidden contents

Signature last update: 2023-04-24

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
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...