XML Newbie question, getting a value from an XML string

XML Newbie question, getting a value from an XML string

Post by Mark Moeyken » Sun, 17 Mar 2002 18:25:04



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

 
 
 

XML Newbie question, getting a value from an XML string

Post by Andy Brow » Mon, 18 Mar 2002 01:42:58


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


 
 
 

XML Newbie question, getting a value from an XML string

Post by Andy Brow » Mon, 18 Mar 2002 01:46:38


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

> 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

 
 
 

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

2. Trailblazer +

3. Newbie... Parse String into A XML Schema/dataset

4. Win Me and dual processors

5. Getting XML as a string

6. Schema and DTD

7. xml-newbie: How to access xml file as embedded resource?

8. Find doesn't search in Addresses or Memo! How to fix?

9. Getting XML document string out of XmlReader

10. Expecting "text/xml" and getting "text/xml" + utf-8 encoding

11. How to set default value of string element to "" in XML shema?

12. String values w/double quotation marks in an XML attribute?

13. How to get a node value or inner text from a xml string using xpath