Jump to content

Recommended Posts

Posted

The standalone="no" directive comes from the _XML_Tidy command.  As far as why the root appears on the same line as the processing instruction, I'll defer to @mLipok on that one.  I have no answer for that.

Posted

???
 

  Quote

    2016/05/18
    "1.1.1.10"
    . NEW: Feature: _XML_Tidy:   if Parameter $sEncoding = -1 then .omitXMLDeclaration = true - mLipok
    .            Feature asked by @GMK here:
    .           https://www.autoitscript.com/forum/topic/176895-xmlau3-v-11109-formerly-xmlwrapperexau3-beta-support-topic/?do=findComment&comment=1294688
 

Expand  

 

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)

Ah, thanks mLipok, I missed that one.  However, what if we want the declaration, but don't want the standalone?

Edited by GMK
Additional question
Posted

Nobody knows why there is no line break - although there is a "& @CRLF" after the first tag?

<?xml version="1.0" encoding="UTF-8" standalone="no"?><MagellanClient>
...

 

Posted (edited)

In this example, instead of showing the Array -> Error 1 ... until I put "xmlns="http://xspf.org/ns/0/" out of the line (please look at my comment-line). It is necessary because of validation of the *.xspf file ( http://xspf.org/xspf-v1.html )

 

#include "XML.au3"
#include <Array.au3>

Global $oXMLDoc = _XML_CreateDOMDocument(Default)
If @error Then
    MsgBox(16, '', _XML_ErrorParser_GetDescription($oXMLDoc))
Else
    Global  $sXML_Content = '<?xml version="1.0" encoding="UTF-8"?>' & @CRLF
            $sXML_Content &= '<playlist version="1" xmlns="http://xspf.org/ns/0/">' & @CRLF ; ->Error 1
            ;$sXML_Content &= '<playlist version="1">' & @CRLF ; No Error
            $sXML_Content &= @TAB & '<trackList>' & @CRLF
            $sXML_Content &= @TAB & @TAB & '<track>' & @CRLF
            $sXML_Content &= @TAB & @TAB & @TAB & '<title>Bach</title>' & @CRLF
            $sXML_Content &= @TAB & @TAB & @TAB & '<creator>Alex Jacobowitz</creator>' & @CRLF
            $sXML_Content &= @TAB & @TAB & '</track>' & @CRLF
            $sXML_Content &= @TAB & '</trackList>' & @CRLF
            $sXML_Content &= '</playlist>'

    _XML_LoadXml($oXMLDoc, $sXML_Content)

    If @error Then
        MsgBox(16, 'Error 0', _XML_ErrorParser_GetDescription($oXMLDoc))
    Else
        _XML_SelectNodes($oXMLDoc, "//track")
        If @error Then
            MsgBox(16, 'Error 1', _XML_ErrorParser_GetDescription($oXMLDoc))
        Else
            Local $iNodesCount = @extended
            For $iItem_idx = 1 To $iNodesCount
                Local $oChilds_Coll = _XML_SelectNodes($oXMLDoc, '//playlist/trackList/track[' & $iItem_idx & ']/*')
                Local $aNodesColl = _XML_Array_GetNodesProperties($oChilds_Coll)
                If @error Then
                    MsgBox(16, 'Error 2', _XML_ErrorParser_GetDescription($oXMLDoc))
                Else
                    _ArrayDisplay($aNodesColl, 'Example_1__XML_SelectNodes ' & $iItem_idx)
                EndIf
            Next
        EndIf
    EndIf
Endif

    Global $sNewXML = _XML_Tidy($oXMLDoc, 'UTF-8')
    MsgBox(0, 'New XML', $sNewXML)

    _XML_LoadXml($oXMLDoc, $sNewXML)
    _XML_Misc_Viewer($oXMLDoc)

 

Edited by TJF
Posted

Very nice @GMK:)

 

 

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

There may be a better way of doing this, but here is what I found so far:

; #FUNCTION# ====================================================================================================================
; Name ..........: _XML_SelectNodes
; Description ...: Selects XML Node(s) based on XPath input from root node.
; Syntax ........: _XML_SelectNodes(ByRef $oXmlDoc, $sXPath)
; Parameters ....: $oXmlDoc             - [in/out] an object. A valid DOMDocument or IXMLDOMElement object.
;                  $sXPath              - a string value. The XML tree path from root node (root/child/child..)
; Return values .: Success              - $oNodesColl - Nodes collection, and set @extended = $oNodesColl.length
;                  Failure              - $XML_RET_FAILURE and sets the @error flag to non-zero (look in #Region XML.au3 - ERROR Enums)
; Author ........: Eltorro
; Modified ......: mLipok, GMK
; Remarks .......:
; Related .......:
; Link ..........:
; Example .......; [yes/no]
; ===============================================================================================================================
Func _XML_SelectNodes(ByRef $oXmlDoc, $sXPath)
    ; Local Error handler declaration, it will be automatic CleanUp when returning from function
    Local $oXML_COM_ErrorHandler = ObjEvent("AutoIt.Error", __XML_ComErrorHandler_InternalFunction)
    #forceref $oXML_COM_ErrorHandler

    __XML_IsValidObject_DOMDocumentOrElement($oXmlDoc)
    If @error Then Return SetError(@error, @extended, $XML_RET_FAILURE)

    If $oXmlDoc.namespaces.length > 0 Then $sXPath = StringRegExpReplace($sXPath, "(?!\[)(\w+)(?!\])", "*[name()='$1']")

    Local $oNodesColl = $oXmlDoc.selectNodes($sXPath)
    If @error Then
        Return SetError($XML_ERR_XPATH, $XML_EXT_DEFAULT, $XML_RET_FAILURE)
    ElseIf (Not IsObj($oNodesColl)) Or $oNodesColl.length = 0 Then
        Return SetError($XML_ERR_EMPTYCOLLECTION, $XML_EXT_DEFAULT, $XML_RET_FAILURE)
    EndIf
    Return SetError($XML_ERR_OK, $oNodesColl.length, $oNodesColl)

EndFunc   ;==>_XML_SelectNodes

 

Posted (edited)

Thank you GMK ! But still no line-break at the beginning of the playlist-tag (like you said):

<?xml version="1.0" encoding="UTF-8" standalone="no"?><playlist version="1" xmlns="http://xspf.org/ns/0/">
    <trackList>
        <track>
            <title>Bach</title>
            <creator>Alex Jacobowitz</creator>
        </track>
    </trackList>
</playlist>

 

Edited by TJF
Posted (edited)

This should give you what you want, @TJF:

; #FUNCTION# ====================================================================================================================
; Name ..........: _XML_LoadXML
; Description ...: Load XML String to the DOMDocument object.
; Syntax ........: _XML_LoadXML(ByRef $oXmlDoc, $sXML_Content[, $sNameSpace = ""[, $bValidateOnParse = True]])
; Parameters ....: $oXmlDoc             - [in/out] an object. A valid DOMDocument object.
;                  $sXML_Content        - a string value. The XML string to load into the document
;                  $sNameSpace          - [optional] a string value. Default is "". The namespace to specifiy if the file uses one.
;                  $bValidateOnParse    - [optional] a boolean value. Default is True. Set the MSXML ValidateOnParse property
; Return values .: Success              - $oXmlDoc
;                  Failure              - $XML_RET_FAILURE and sets the @error flag to non-zero (look in #Region XML.au3 - ERROR Enums)
; Author ........: Eltorro, Lukasz Suleja, Tom Hohmann
; Modified ......: mLipok, GMK
; Remarks .......:
; Related .......:
; Link ..........;
; Example .......; [yes/no]
; ===============================================================================================================================
Func _XML_LoadXML(ByRef $oXmlDoc, $sXML_Content, $sNameSpace = "", $bValidateOnParse = True)
    ; Local Error handler declaration, it will be automatic CleanUp when returning from function
    Local $oXML_COM_ErrorHandler = ObjEvent("AutoIt.Error", __XML_ComErrorHandler_InternalFunction)
    #forceref $oXML_COM_ErrorHandler

    __XML_IsValidObject_DOMDocument($oXmlDoc)
    If @error Then Return SetError(@error, @extended, $XML_RET_FAILURE)

    If _XML_Misc_GetDomVersion() > 4 Then $oXmlDoc.setProperty("ProhibitDTD", False)
    $oXmlDoc.async = False
    $oXmlDoc.preserveWhiteSpace = False
    $oXmlDoc.validateOnParse = $bValidateOnParse
    $oXmlDoc.LoadXml($sXML_Content)
    If $oXmlDoc.parseError.errorCode Then
        Return SetError($XML_ERR_PARSE, $oXmlDoc.parseError.errorCode, $XML_RET_FAILURE)
    EndIf

    ; SelectionLanguage do not use this as this cause a problem
    ; $oXmlDoc.setProperty("SelectionLanguage", "XPath")

    $oXmlDoc.setProperty("SelectionNamespaces", $sNameSpace)
    ; TODO this following line was here, actualy I (mLipok) wondering why.
    ; "xmlns:ms='urn:schemas-microsoft-com:xslt'"
    ; here I put some reference to look in
    ; https://msdn.microsoft.com/en-us/library/ms256186(v=vs.110).aspx

    Return SetError($XML_ERR_OK, $XML_EXT_DEFAULT, $oXmlDoc)

EndFunc   ;==>_XML_LoadXML

 

Edited by GMK
Add credit
Posted (edited)

@GMK as I see you change :

$oXmlDoc.preserveWhiteSpace = True

to

$oXmlDoc.preserveWhiteSpace = False

is that all needed changes ?

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

Oh... I see at the Array $aNodesColl :

Col 5

<title xmlns="http://xspf.org/ns/0/">Bach</title>
<creator xmlns="http://xspf.org/ns/0/">Alex Jacobowitz</creator>

 

  • 2 weeks later...
Posted

Hi,

i'm new to autoit and new to xml.

I'd like to create a XML file with the wrapper .... but still have  some problems.

Not sure if this is a wrapper problem (Probably not. :-))

With this code snippet:

;create xml object
    Local $oXMLDoc = _XML_CreateDOMDocument()
    if @error Then 
        ConsoleWrite("Error _XML_CreateDOMDocument. Error: " & @error & " Extended: " & @extended & @LF)
    EndIf

    local $sXML_Content = '<?xml version="1.0" encoding="UTF-8" standalone="yes"?> ' & @CRLF
    $sXML_Content &= '<FILE xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">' & @CRLF
    $sXML_Content &= '</FILE>'
    
    _XML_LoadXml($oXMLDoc, $sXML_Content)
    
    $aDeptCodeTemp[0][0] = "DEPT_CODE"
    $aDeptCodeTemp[0][1] = "1"
    
    $sXMLString = "FILE"
    
    ; first child 'record'
    _XML_CreateChildWAttr($oXMLDoc, $sXMLString, "RECORD", $aDeptCodeTemp)
        
    ; second child "Field1"
    $sXMLString &= "/" & "Field1"
    _XML_InsertChildNode($oXMLDoc, $sXMLString, "FIELD1")
    
    $sXMLValue = _XML_Tidy($oXMLDoc, 'UTF-8')
    _XML_LoadXml($oXMLDoc, $sXMLValue)


    _XML_SaveToFile($oXMLDoc, $aFilePath)

I get this file:

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<FILE xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <RECORD DEPT_CODE="01">
        <FIELD1/>
    </RECORD>
</FILE>

I need more 'RECORD' nodes with the same data in it.

;create xml object
    Local $oXMLDoc = _XML_CreateDOMDocument()
    if @error Then 
        ConsoleWrite("Error _XML_CreateDOMDocument. Error: " & @error & " Extended: " & @extended & @LF)
    EndIf

    local $sXML_Content = '<?xml version="1.0" encoding="UTF-8" standalone="yes"?> ' & @CRLF
    $sXML_Content &= '<FILE xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">' & @CRLF
    $sXML_Content &= '</FILE>'
    
    _XML_LoadXml($oXMLDoc, $sXML_Content)
    
    $aDeptCodeTemp[0][0] = "DEPT_CODE"
    $aDeptCodeTemp[0][1] = "1"
    
    $sXMLString = "FILE"
    
    ; first child 'record'
    _XML_CreateChildWAttr($oXMLDoc, $sXMLString, "RECORD", $aDeptCodeTemp)
        
    ; second child "Field1"
    $sXMLString &= "/" & "Field1"
    _XML_InsertChildNode($oXMLDoc, $sXMLString, "FIELD1")
    
    
    $aDeptCodeTemp[0][0] = "DEPT_CODE"
    $aDeptCodeTemp[0][1] = "2"
    
    $sXMLString = "FILE"
    
    ; first child 'record'
    _XML_CreateChildWAttr($oXMLDoc, $sXMLString, "RECORD", $aDeptCodeTemp)
        
    ; second child "Field1"
    $sXMLString &= "/" & "Field1"
    _XML_InsertChildNode($oXMLDoc, $sXMLString, "FIELD1")
    
    
    $sXMLValue = _XML_Tidy($oXMLDoc, 'UTF-8')
    _XML_LoadXml($oXMLDoc, $sXMLValue)


    _XML_SaveToFile($oXMLDoc, $aFilePath)

Give me this file:

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<FILE xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <RECORD DEPT_CODE="01">
        <FIELD1/>
        <FIELD1/>
    </RECORD>
    <RECORD DEPT_CODE="02">
        <FIELD1/>
    </RECORD>
</FILE>

In the first "RECORD" node is one "FIELD1" child-node too much.

Pretty clear why ... but no clue how to fix it. Where is my mistake? 

How can i insert a child-node only in the last "RECORD" node?

Thx for some hints ...

 

Posted

I will look at this when I back to home (today).

 

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)

New version:
 

  Quote

    2016/10/27
    "1.1.1.12"

    . Changed: Function: _XML_SetAttrib - support for $vAttributeNameOrList - GMK
    . Added: Enums:  $XMLATTR_COLNAME, $XMLATTR_COLVALUE, $XMLATTR_COLCOUNTER - mLipok
    . Changed: Function: _XML_GetAllAttrib - array result is reordered ROWS<>COLS - mLipok
    .            now are coherent manner for: _XML_InsertChildWAttr, _XML_CreateChildWAttr, _XML_SetAttrib, _XML_GetAllAttrib
    .              THIS IS !!! SCRIPT BREAKING CHANGE !!!
    . Added: Function parameter: _XML_Load new parameter $bPreserveWhiteSpace = True - GMK
    . Added: Function parameter: _XML_LoadXML new parameter $bPreserveWhiteSpace = True - GMK
    . Changed: Enums:  $XML_ERR_OK >> $XML_ERR_SUCCESS - for unification/coherence in relatation to some other UDF's - mLipok
    .
    . EXAMPLES: New, and checked/refactored/fixed
    .    XML__Examples_TIDY2.au3
    .    XML__Examples_User_BlaBlaFoo__Dellwarranty.au3
    .    XML__Examples_User_Shrapnel.au3

Expand  

Download link. 

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

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
×
×
  • Create New...