Jump to content

XML to Array


acmecd
 Share

Recommended Posts

Hello.
I have the following XML code, but unfortunately, it can't read the in the array.
Read each node in array should be worn separately and then read atributes value.

I tried multiple code samples of the forum, but nothing is working, maybe someone has a sample or idea.

 

<views><acc version="6.12.0.0"/><view name="Contacts/Contacts"><item><field name="Related Application" class="INFO" value="vnd.sec.contact.phone"/><field name="Name" class="CONTACT" value="Buc"/><field name="Mobile" class="PHONE" value="220-258-759-22"/><field name="Index" class="INFO" value="1"/><field name="Account Name" class="INFO" value="vnd.sec.contact.phone"/></item><item><field name="Related Application" class="INFO" value="vnd.sec.contact.phone"/><field name="Name" class="CONTACT" value="RQASAS2"/><field name="Mobile" class="PHONE" value="296-040-567-833"/><field name="Index" class="INFO" value="2"/><field name="Account Name" class="INFO" value="vnd.sec.contact.phone"/></item>

Link to comment
Share on other sites

  • Moderators

@acmecd in the future please watch where you are posting. The Examples forum clearly states it is not a help and support forum for asking questions.

"Profanity is the last vestige of the feeble mind. For the man who cannot express himself forcibly through intellect must do so through shock and awe" - Spencer W. Kimball

How to get your question answered on this forum!

Link to comment
Share on other sites

did you try this: _XML_Array_GetNodesProperties($oNodesColl)

??

 

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:

Spoiler

Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind. 

My contribution (my own projects): * Debenu Quick PDF Library - UDF * Debenu PDF Viewer SDK - UDF * Acrobat Reader - ActiveX Viewer * UDF for PDFCreator v1.x.x * XZip - UDF * AppCompatFlags UDF * CrowdinAPI UDF * _WinMergeCompare2Files() * _JavaExceptionAdd() * _IsBeta() * Writing DPI Awareness App - workaround * _AutoIt_RequiredVersion() * Chilkatsoft.au3 UDF * TeamViewer.au3 UDF * JavaManagement UDF * VIES over SOAP * WinSCP UDF * GHAPI UDF - modest begining - comunication with GitHub REST APIErrorLog.au3 UDF - A logging Library * Include Dependency Tree (Tool for analyzing script relations) * Show_Macro_Values.au3 *

 

My contribution to others projects or UDF based on  others projects: * _sql.au3 UDF  * POP3.au3 UDF *  RTF Printer - UDF * XML.au3 UDF * ADO.au3 UDF SMTP Mailer UDF * Dual Monitor resolution detection * * 2GUI on Dual Monitor System * _SciLexer.au3 UDF * SciTE - Lexer for console pane

Useful links: * Forum Rules * Forum etiquette *  Forum Information and FAQs * How to post code on the forum * AutoIt Online Documentation * AutoIt Online Beta Documentation * SciTE4AutoIt3 getting started * Convert text blocks to AutoIt code * Games made in Autoit * Programming related sites * Polish AutoIt Tutorial * DllCall Code Generator * 

Wiki: Expand your knowledge - AutoIt Wiki * Collection of User Defined Functions * How to use HelpFile * Good coding practices in AutoIt * 

OpenOffice/LibreOffice/XLS Related: WriterDemo.au3 * XLS/MDB from scratch with ADOX

IE Related:  * How to use IE.au3  UDF with  AutoIt v3.3.14.x * Why isn't Autoit able to click a Javascript Dialog? * Clicking javascript button with no ID * IE document >> save as MHT file * IETab Switcher (by LarsJ ) * HTML Entities * _IEquerySelectorAll() (by uncommon) * IE in TaskSchedulerIE Embedded Control Versioning (use IE9+ and HTML5 in a GUI) * PDF Related:How to get reference to PDF object embeded in IE * IE on Windows 11

I encourage you to read: * Global Vars * Best Coding Practices * Please explain code used in Help file for several File functions * OOP-like approach in AutoIt * UDF-Spec Questions *  EXAMPLE: How To Catch ConsoleWrite() output to a file or to CMD *

I also encourage you to check awesome @trancexx code:  * Create COM objects from modules without any demand on user to register anything. * Another COM object registering stuffOnHungApp handlerAvoid "AutoIt Error" message box in unknown errors  * HTML editor

winhttp.au3 related : * https://www.autoitscript.com/forum/topic/206771-winhttpau3-download-problem-youre-speaking-plain-http-to-an-ssl-enabled-server-port/

"Homo sum; humani nil a me alienum puto" - Publius Terentius Afer
"Program are meant to be read by humans and only incidentally for computers and execute" - Donald Knuth, "The Art of Computer Programming"
:naughty:  :ranting:, be  :) and       \\//_.

Anticipating Errors :  "Any program that accepts data from a user must include code to validate that data before sending it to the data store. You cannot rely on the data store, ...., or even your programming language to notify you of problems. You must check every byte entered by your users, making sure that data is the correct type for its field and that required fields are not empty."

Signature last update: 2023-04-24

Link to comment
Share on other sites

You can do something like this...maybe not sure if you want to separate the items by groups:

$sXML = '<root><item><field name="Related Application" class="INFO" value="vnd.sec.contact.phone"/><field name="Name" class="CONTACT" value="Buc"/><field name="Mobile" class="PHONE" value="220-258-759-22"/><field name="Index" class="INFO" value="1"/><field name="Account Name" class="INFO" value="vnd.sec.contact.phone"/></item><item><field name="Related Application" class="INFO" value="vnd.sec.contact.phone"/><field name="Name" class="CONTACT" value="RQASAS2"/><field name="Mobile" class="PHONE" value="296-040-567-833"/><field name="Index" class="INFO" value="2"/><field name="Account Name" class="INFO" value="vnd.sec.contact.phone"/></item></root>'
Local $oXML = ObjCreate("Microsoft.XMLDOM")
$oXML.loadxml($sXML)
$oItems = $oXML.SelectNodes("//field")
Enum $iName, $iClass, $iValue, $iUBound

Local $a[$oItems.length][$iUBound]
$iCounter = 0
For $oItem In $oItems

;~  $oFields = $oItem.SelectNodes("./field")
;~  For $oField In $oFields

        $a[$iCounter][$iName]   = $oItem.getattribute("name")
        $a[$iCounter][$iClass]  = $oItem.getattribute("class")
        $a[$iCounter][$iValue]  = $oItem.getattribute("value")
;~  Next
    $iCounter += 1
Next
_ArrayDisplay($a)

 

IEbyXPATH-Grab IE DOM objects by XPATH IEscriptRecord-Makings of an IE script recorder ExcelFromXML-Create Excel docs without excel installed GetAllWindowControls-Output all control data on a given window.
Link to comment
Share on other sites

Thanks for the advice.
The solution is to work on.

The only improvement that would be useful, if can get data from a particular node.
For example, can choose from  <item>...atributes.... </item>  5, 20, 30 etc nodes, because in this case, all attributes are read from nodes  / / file.

In any event, thank you for your feedback and help.

Link to comment
Share on other sites

You would need to make the xpath specific to what you want.  Anything is possible, you just need to define parameters.  Do you really only want the first 5, 20, 30 etc nodes, or specific nodes based on some value.

You could also add in an if statement within the loop, or an exitloop once a certain count is hit.

IEbyXPATH-Grab IE DOM objects by XPATH IEscriptRecord-Makings of an IE script recorder ExcelFromXML-Create Excel docs without excel installed GetAllWindowControls-Output all control data on a given window.
Link to comment
Share on other sites

XML files that need to be processed is the phone book. As can be seen from the examples, each file has its own structure, but the contact data is between nodes    <item>...atributes.... </item>

In this case, select the relevant contact and then read its contents. Each data file is different, depending on  phone model.

Examples:

<item><field name="Index" class="INFO" value="35"/><field name="Name" class="CONTACT" value="Max.Valmer."/><field name="Tel" class="PHONE" value="282-591-159-654"/><field name="Storage" class="INFO" value="SIM"/></item

 

<item><field name="Name" class="CONTACT" value="Dins"/><field name="Index" class="INFO" value="37"/><field name="Mobile" class="PHONE" value="282-891-159-615"/></item>

 

<item><field name="Related Application" class="INFO" value="com.viber.voip.account"/><field name="Name" class="CONTACT" value="aurasD"/><field name="Contact" class="CONTACT" value="532"/><field name="Storage" class="INFO" value="Device"/><field name="Index" class="INFO" value="1052"/><field name="Account Name" class="INFO" value="382-894-159-657"/></item>

I believe the only solution without regard to the file structure is read node and then each process.

 
Thank you in advance for assistance

Link to comment
Share on other sites

#include <Array.au3>
$sXML = '<root><item><field name="Related Application" class="INFO" value="vnd.sec.contact.phone"/><field name="Name" class="CONTACT" value="Buc"/><field name="Mobile" class="PHONE" value="220-258-759-22"/><field name="Index" class="INFO" value="1"/><field name="Account Name" class="INFO" value="vnd.sec.contact.phone"/></item><item><field name="Related Application" class="INFO" value="vnd.sec.contact.phone"/><field name="Name" class="CONTACT" value="RQASAS2"/><field name="Mobile" class="PHONE" value="296-040-567-833"/><field name="Index" class="INFO" value="2"/><field name="Account Name" class="INFO" value="vnd.sec.contact.phone"/></item></root>'
Local $oXML = ObjCreate("Microsoft.XMLDOM")
$oXML.loadxml($sXML)
$sSearchContact = "Buc"

$oItems = $oXML.SelectNodes("//field[@class='CONTACT' and @value='" & $sSearchContact & "']")
Enum $iName, $iClass, $iValue, $iUBound



Local $a
$iCounter = 0
For $oItem In $oItems

    $oParent = $oItem.parentNode



    $oFields = $oParent.SelectNodes("./field")
    If IsObj($oFields) Then
        If IsArray($a) Then
            ReDim $a[UBound($a)+$oFields.length][$iUBound]
        Else
            Local $a[$oFields.length][$iUBound]
        EndIf

        For $oField In $oFields

            $a[$iCounter][$iName]   = $oField.getattribute("name")
            $a[$iCounter][$iClass]  = $oField.getattribute("class")
            $a[$iCounter][$iValue]  = $oField.getattribute("value")
            $iCounter += 1
        Next
    EndIf



Next
_ArrayDisplay($a)

output for contact=Buc

[0]|Related Application|INFO|vnd.sec.contact.phone
[1]|Name|CONTACT|Buc
[2]|Mobile|PHONE|220-258-759-22
[3]|Index|INFO|1
[4]|Account Name|INFO|vnd.sec.contact.phone
 

Edited by jdelaney
IEbyXPATH-Grab IE DOM objects by XPATH IEscriptRecord-Makings of an IE script recorder ExcelFromXML-Create Excel docs without excel installed GetAllWindowControls-Output all control data on a given window.
Link to comment
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
 Share

  • Recently Browsing   0 members

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