Wrox Programmer Forums
Go Back   Wrox Programmer Forums > XML > XML
|
XML General XML discussions.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the XML section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old July 21st, 2006, 06:03 PM
Registered User
 
Join Date: Jul 2006
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Default extracting atributes - how to do this?

Hi

I have following XML file:

===========================
- <CountriesCities>
- <Countries>
<CoID>111</CoID>
<Country>ALBANIA</Country>
<Cities CiID="1478" City="TIRANA" />
</Countries>
- <Countries>
<CoID>1</CoID>
<Country>AUSTRALIA</Country>
<Cities CiID="1" City="ADELAIDE" />
<Cities CiID="4" City="MELBOURNE" />
<Cities CiID="6" City="SYDNEY" />
</Countries>
- <Countries>
=============================

Using this Code:

=============================
Set objXMLDoc = CreateObject("Microsoft.XMLDOM")
objXMLDoc.async = False
objXMLDoc.load Server.MapPath("../test/countries.xml")
varCountry = 1

set nodes=objXMLDoc.selectNodes("/CountriesCities/Countries[CoID='"&varCountry&"']")

for each x in nodes
response.write(x.xml) & "<br>"
next
=============================

When varCountry = 1
I get this result:

1 AUSTRALIA

If the varCountry is 111
I get this result:

111 ALBANIA

=============================

I would like to get if varCountry = 1

1 ADELAIDE
4 MELBOURNE
6 SYDNEY

and If VarCountry = 111

1478 TIRANA

Anyone can point me in some direction?

Regards, Zoreli

 
Old July 22nd, 2006, 04:54 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

Add /Cities to the end of your path expression, then select the required attributes from your application code using getAttribute().

Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
 
Old July 22nd, 2006, 05:21 AM
Registered User
 
Join Date: Jul 2006
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi

I am trying something like this :

==============================================

Set objXMLDoc = CreateObject("Microsoft.XMLDOM")
objXMLDoc.async = False
objXMLDoc.load Server.MapPath("../test/countries.xml")
varCountry = 2

set nodes=objXMLDoc.selectNodes("/CountriesCities/Countries[CoID='"&varCountry&"']/Country")

    for each x in nodes
        response.write(x.xml)
            Set states = objXMLDoc.getElementsByTagName("Cities")
                n_states = states.length
                    For i = 1 To n_states

                        Set state = states.NextNode
                        attr = state.getAttribute("CiID")
                        attr2 = state.getAttribute("City")
                        Response.write(attr +"&nbsp;"+ attr2 + "<br>")

                    Next
    next


==============================================
But without any sucess. What I am doing wrong?

Regards, Zoran

 
Old July 22nd, 2006, 05:59 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

getElementsByTagName gets all the Cities in the document, not just those that are children of "x".

Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
 
Old July 22nd, 2006, 06:22 AM
Registered User
 
Join Date: Jul 2006
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi Michael

Thanks for the help. However, I am looking at this peace of the code so many hours, and I can't find the solution. :(

Can you please help me with some snipet code?

This is my first serous project with XML and I have problem to figure out how the thins works in XML.

Thanks


 
Old July 22nd, 2006, 07:51 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

I don't actually program in VB myself so I can't give you the exact code, all I can do is spot your obvious errors.

Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
 
Old August 12th, 2006, 03:46 AM
Authorized User
 
Join Date: Aug 2006
Posts: 22
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi,
I do not program in VB but in C# and I only can help you with a peace of console code that I wrote to explain you how you can do it. The only thing you have to do is try to implement it in VB.

Below is the XML file I have used (named test.xml), copied from above:

<?xml version="1.0" encoding="utf-8" ?>
<CountriesCities>
    <Countries>
          <CoID>111</CoID>
          <Country>ALBANIA</Country>
          <Cities CiID="1478" City="TIRANA"></Cities>
    </Countries>
    <Countries>
      <CoID>1</CoID>
      <Country>AUSTRALIA</Country>
      <Cities CiID="1" City="ADELAIDE"></Cities>
      <Cities CiID="4" City="MELBOURNE"></Cities>
      <Cities CiID="6" City="SYDNEY"></Cities>
    </Countries>
</CountriesCities>

and here is the code in C#:

using System;
using System.Xml;

public class test
{
    public static void Main()
    {
       XmlDataDocument xmlddoc = new XmlDataDocument();
       xmlddoc.Load("test.xml");
       XmlNodeList nlst = xmlddoc.DocumentElement.SelectSingleNode("//Countries[CoID=1]").SelectNodes("Cities");
       foreach (XmlNode x in nlst)
       {
           Console.Write(x.Attributes.GetNamedItem("CiID").Va lue+" ");
           Console.Write(x.Attributes.GetNamedItem("City").Va lue+"\n");
       }
    }
}

and of course the output:

C:\>test
1 ADELAIDE
4 MELBOURNE
6 SYDNEY

Regards,
.NETmateur
Electrical Engineer





Similar Threads
Thread Thread Starter Forum Replies Last Post
extracting attribute value hag XSLT 1 March 3rd, 2008 03:16 AM
string extracting fraijo SQL Server 2000 4 October 25th, 2006 09:53 PM
Extracting Text Jeff Mason BOOK: Beginning Regular Expressions 1 October 24th, 2006 06:38 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.