Indigo Rose Software

Professional Software Development Tools

 
Results 1 to 7 of 7
  1. #1
    Join Date
    Apr 2010
    Posts
    529

    XML - feed all values to table

    Hey all,

    Tried to figure this out in the help, but no luck - so I'm here!

    I'm trying to figure out how to retrieve all of the values of a specific element within an XML.

    For example, I want to retrieve all "first_name" values from an XML and feed it into a table. As the data is added to the XML, I have no way of knowing how many "first_name" values there will be, but expect I need to iterate through and find all matching values and read them, but ... now I'm already lost!

    Any ideas?

    Cheers,
    Dean

  2. #2
    Join Date
    Feb 2007
    Location
    Como, Italy
    Posts
    1,415
    Hi Dean,

    I've not yet used XML functions, but as far I can understand your question, the following except from the user manual I think it could be useful for you. Please tell me if I clearly understood your question.

    XML Paths
    Many of the XML actions contain a parameter called XMLPath. This path is used to locate a specific element in the XML file. For example, in the sample XML document, the "address" element inside the first "customer" element can be accessed using:

    database/customer/address_info/address

    Since an XML document can contain multiple elements with the same name, you often cannot access every element in a document by name alone. For example, what if you need to access the information in the second customer element?

    The secret is to add an optional index to each element name in order to specify which occurrence of that name the path refers to. In XML paths, an index consists of a single colon ( followed by a number. (If no index is specified, the index is assumed to be 1.)

    For example, to access the second "customer" element in the first "database" element of the sample XML document, you could use:

    database/customer:2

    As another example, to access the second "phone" element in the second "customer" element, you would use:

    database/customer:2/phone:2

    Matching Any Element Name
    An asterisk (*) can be used in an XML path to find the first matching element at a given level.

    For example, you can use paths like this:

    */customer

    ...which refers to the first customer element in the first root element, no matter what the root element is named. In other words, the above path wouldn't care whether the root element was "database" or "moonbase_alpha."

    You can use an asterisk with an index to refer to an element by position instead of by name. For example, you could use the following path to access the second element inside the customer element, no matter what that element is named:

    database/customer/*:2

    Using the above path, it wouldn't matter what the first two elements inside customer were named; they could be "first_name" and "last_name" or they could be "nose_length" and "hair_color"...either way, it would be the second element that would be referenced.

    You can even form a path with nothing but asterisks. For example, the following path would refer to the third child element within the second root element:

    *:2/*:3

    Note: An asterisk always matches a single element. It doesn't match "all" elements at that level.
    Cheers,
    Emanuele
    We are slowly invading your planet to teach lazy humans to read the user manual.
    But don't be scared: we are here to help.

  3. #3
    Join Date
    Apr 2010
    Posts
    529
    Hi Emanuele,

    I read that part, but it seems to be a way to get one specific entry. To me the example is like retrieving a particular record from a database (not knowing all the database field names), but I want to get the information in one field from every database record.

    Quote Originally Posted by Cybergraph View Post
    You can use an asterisk with an index to refer to an element by position instead of by name. For example, you could use the following path to access the second element inside the customer element, no matter what that element is named:

    database/customer/*:2

    Using the above path, it wouldn't matter what the first two elements inside customer were named; they could be "first_name" and "last_name" or they could be "nose_length" and "hair_color"...either way, it would be the second element that would be referenced.
    I guess I want to get something like:
    database/customer/first_name:* instead of
    database/customer/ *:2

    I think maybe I will have to re-read that part tomorrow again.

    Cheers,
    Dean
    Last edited by MadDogDean; 02-09-2011 at 04:47 AM.

  4. #4
    Join Date
    May 2009
    Posts
    336
    Use this:
    Code:
    tblCount = XML.Count("database/customer", "*");
    Last edited by .74; 02-09-2011 at 08:28 AM.

  5. #5
    Join Date
    May 2009
    Posts
    336
    Okay.. this should work:
    Code:
    XML.Load(C:\\test.xml");
    tblCount = XML.Count("database", "customer");
    for i = 1, tblCount do
    	local strFirstname = XML.GetValue("database/customer:"..i.."/firstname");
    	ListBox.AddItem("ListBox1", strFirstname, i);
    end

  6. #6
    Join Date
    Oct 2010
    Posts
    125
    XML is one of my strong points and .74 has the solution that best fits your needs. I have tweaked the example to make it easier to understand and more suitable for your needs.

    Code:
    XML.Load(example.xml");
    
    count = XML.Count("database", "customers");
    
    for x=1, count do
    	
            local firstname = XML.GetValue("database/customer:"..x.."/first_name");
    	
            ListBox.AddItem("ListBox1", first_name, x);
    
    end
    Nathaniel Blackburn
    Shadowscape Studios
    Last edited by shadowscape; 02-09-2011 at 09:35 AM.

  7. #7
    Join Date
    Oct 2010
    Posts
    125
    i made a slight mistake in my previous post, sorry and here is the revised code to make it up to you, i really should make a xml tutorial when i have some spare time.

    Code:
    XML.Load("example.xml");
    
    count = XML.Count("database", "customers");
    
    for x=1, count do
    	
            local firstname = XML.GetValue("database/customers:"..x.."/first_name");
    	
            ListBox.AddItem("ListBox1", first_name, x);
    
    end
    Nathaniel Blackburn
    Shadowscape Studios
    Last edited by shadowscape; 02-09-2011 at 09:44 AM.

  8. #8
    Join Date
    Oct 2009
    Location
    Merton, United Kingdom
    Posts
    684
    Quote Originally Posted by shadowscape View Post
    i made a slight mistake in my previous post, sorry and here is the revised code to make it up to you, i really should make a xml tutorial when i have some spare time.

    Code:
    XML.Load(example.xml");
    
    count = XML.Count("database", "customers");
    
    for x=1, count do
    	
            local firstname = XML.GetValue("database/customers:"..x.."/first_name");
    	
            ListBox.AddItem("ListBox1", first_name, x);
    
    end
    Nathaniel Blackburn
    Shadowscape Studios
    Might want to check out line 1 too. :P

  9. #9
    Join Date
    Apr 2010
    Posts
    529
    Hey all,

    Thanks for the input, that helps to move things along.

    Quote Originally Posted by shadowscape

    [CODE
    XML.Load("example.xml");

    count = XML.Count("database", "customers");

    for x=1, count do

    local firstname = XML.GetValue("database/customers:"..x.."/first_name");

    ListBox.AddItem("ListBox1", first_name, x);

    end
    [/CODE]
    When you defined "local firstname..." shouldn't the variable in the next line also be "firstname"

    Code:
            local firstname = XML.GetValue("database/customers:"..x.."/first_name");
    	
            ListBox.AddItem("ListBox1", firstname, x);
    Cheers,
    Dean

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts