XML Newbie question, getting a value from an XML string 
Author Message
 XML Newbie question, getting a value from an XML string

I have a web service that returns an xml string:
<USERS>
   <UserID>1</UserID>
   <FirstName>Mark</FirstName>
   <LastName>Moeykens</LastName>
</USERS>

So now that I have this string, how do I find out what the value is for the
field "UserID"?

Just to let you know I have no experience with XML so if you reply with
something like "Just use DOM" I still won't know what to do.

If it is easier I can have the web service return a string like this too:
<USERS UserID="1" FirstName="Mark" LastName="Moeykens"/>

I've been working at this for the last 3 hours and am getting overwhelmed by
the whole XML world.

I've managed to read the string into an XmlDocument object but that's about
it.

Thanks,
Mark Moeykens



Wed, 01 Sep 2004 17:25:04 GMT  
 XML Newbie question, getting a value from an XML string
If you are using MSXML for parsing the XML, then it might look like this
(assuming you have the XML string)...

-----------
Public Function GetUserID(xmlstring) as String

    Dim xDoc As MSXML2.DOMDocument
    Dim tempid as string

    '//load the xml string into a DOM
    set xDoc = new MSXML2.DOMDocument

    '//get the text value of the first element named UserID
    '//the string in the selectSingleNodes function is an XPath statement

    tempid = xDoc.selectSingleNode("/Users/UserID").Text

    '//you can also use this if you want to get the userid from an element's
attribute
    '//the string in the selectSingleNodes function is an XPath statement


    GetUserID = tempid

End function
---------

Note that I've deliberately left out trapping parser errors upon load. This
should be inserted after the xDoc.loadXML statement.

        'XML PARSING ERROR HANDLER
        If Err.Number <> 0 Then

            strErr = Err.Description & vbCrLf
            strErr = strErr & xDoc.parseError.reason & " line: " &
xmlSource.parseError.Line & " col: " & xDoc.parseError.linepos & " text: " &
_                           xDoc.parseError.srcText
            MsgBox strErr, vbCritical, "Error loading the XML"

            GetUserID = ""
            exit function

        End If

    set xNode = xDoc.GetSingleNode("//UserID").Tex

Quote:
> I have a web service that returns an xml string:
> <USERS>
>    <UserID>1</UserID>
>    <FirstName>Mark</FirstName>
>    <LastName>Moeykens</LastName>
> </USERS>

> So now that I have this string, how do I find out what the value is for
the
> field "UserID"?

> Just to let you know I have no experience with XML so if you reply with
> something like "Just use DOM" I still won't know what to do.

> If it is easier I can have the web service return a string like this too:
> <USERS UserID="1" FirstName="Mark" LastName="Moeykens"/>

> I've been working at this for the last 3 hours and am getting overwhelmed
by
> the whole XML world.

> I've managed to read the string into an XmlDocument object but that's
about
> it.

> Thanks,
> Mark Moeykens



Thu, 02 Sep 2004 00:42:58 GMT  
 XML Newbie question, getting a value from an XML string
Sorry... left out a statement:

If you are using MSXML for parsing the XML, then it might look like this
(assuming you have the XML string)...

-----------
Public Function GetUserID(xmlstring) as String

    Dim xDoc As MSXML2.DOMDocument
    Dim tempid as string

    '//load the xml string into a DOM
    set xDoc = new MSXML2.DOMDocument

Quote:
>>>>'You need this here...
>>>> xDoc.loadXML xmlstring

    '//get the text value of the first element named UserID
    '//the string in the selectSingleNodes function is an XPath statement

    tempid = xDoc.selectSingleNode("/Users/UserID").Text

    '//you can also use this if you want to get the userid from an element's
attribute
    '//the string in the selectSingleNodes function is an XPath statement


    GetUserID = tempid

End function
---------

Note that I've deliberately left out trapping parser errors upon load. This
should be inserted after the xDoc.loadXML statement.

        'XML PARSING ERROR HANDLER
        If Err.Number <> 0 Then

            strErr = Err.Description & vbCrLf
            strErr = strErr & xDoc.parseError.reason & " line: " &
xmlSource.parseError.Line & " col: " & xDoc.parseError.linepos & " text: " &
_                           xDoc.parseError.srcText
            MsgBox strErr, vbCritical, "Error loading the XML"

            GetUserID = ""
            exit function

        End If

    set xNode = xDoc.GetSingleNode("//UserID").Tex

Quote:
> I have a web service that returns an xml string:
> <USERS>
>    <UserID>1</UserID>
>    <FirstName>Mark</FirstName>
>    <LastName>Moeykens</LastName>
> </USERS>

> So now that I have this string, how do I find out what the value is for
the
> field "UserID"?

> Just to let you know I have no experience with XML so if you reply with
> something like "Just use DOM" I still won't know what to do.

> If it is easier I can have the web service return a string like this too:
> <USERS UserID="1" FirstName="Mark" LastName="Moeykens"/>

> I've been working at this for the last 3 hours and am getting overwhelmed
by
> the whole XML world.

> I've managed to read the string into an XmlDocument object but that's
about
> it.

> Thanks,
> Mark Moeykens




- Show quoted text -

Quote:
> I have a web service that returns an xml string:
> <USERS>
>    <UserID>1</UserID>
>    <FirstName>Mark</FirstName>
>    <LastName>Moeykens</LastName>
> </USERS>

> So now that I have this string, how do I find out what the value is for
the
> field "UserID"?

> Just to let you know I have no experience with XML so if you reply with
> something like "Just use DOM" I still won't know what to do.

> If it is easier I can have the web service return a string like this too:
> <USERS UserID="1" FirstName="Mark" LastName="Moeykens"/>

> I've been working at this for the last 3 hours and am getting overwhelmed
by
> the whole XML world.

> I've managed to read the string into an XmlDocument object but that's
about
> it.

> Thanks,
> Mark Moeykens



Thu, 02 Sep 2004 00:46:38 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. getting the values from an XML string using ASP

2. Newbie - Creating/Editing XML Values

3. Getting names from db with id's from xml (dataset join question)

4. Date problems with XML in ASP.NET / VB.NET- newbie question

5. samples for transforming an ASN1 string to XML string

6. XML, Converting Word doc's to XML files (O'2000)

7. How to create a secure connection, send an xml msg and receive and xml response

8. Render XML+XSL into PrintDocument (not XML) ?

9. Parsing XML from an ASP page that Creates XML fails

10. Trying to append a simple XML document as a node in an XML document

11. checking for xml tag/xml data???

12. XML From SQL2000 (Probs with FOR XML EXPLICIT)

 

 
Powered by phpBB® Forum Software