acmecd

XML to Array

9 posts in this topic

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>

Share this post


Link to post
Share on other sites



@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.


√-1 2^3 ∑ π, and it was delicious!

Share this post


Link to post
Share on other sites

did you try this: _XML_Array_GetNodesProperties($oNodesColl)

??

 


Wondering who uses AutoIT and what it can be used for ?


ADO.au3 UDF     POP3.au3 UDF     XML.au3 UDF    How to use IE.au3  UDF with  AutoIt v3.3.14.x  for other useful stuff click the following button:

Spoiler

Last update: 2017-03-27
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 SOAPPOP3.au3 UDF *  RTF Printer - UDF * XML.au3 - BETA * ADO.au3 UDF SMTP Mailer UDF * WinSCP UDF *

My contribution to others projects: * _sql.au3 UDF  *

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 * Best coding practices * 

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 *

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 *

"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."

 

Share this post


Link to post
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.

Share this post


Link to post
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.

Share this post


Link to post
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.

Share this post


Link to post
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

Share this post


Link to post
Share on other sites

#8 ·  Posted (edited)

#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.

Share this post


Link to post
Share on other sites

 
Thank you very much, i finally began to understand XML structure, problem solved.

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