Microsoft.XMLDOM ASP XML parsing

Discussion in 'ASP General' started by B, Jan 13, 2006.

  1. B

    B Guest

    I have an XML string

    <?xml version="1.0" encoding="UTF-8"?>
    <SSOUser><Status>FAIL</Status><Message>Unable to find session id of
    1137018716939</Message></SSOUser>


    That I am trying to retrieve the values between the tags for.
    I want to get the value from
    <Status>FAIL</Status>
    so I get the value "FAIL"

    I am using the following code (and have tried MANY variations) to retrieve
    the specific field values but I can not find out how to do this.


    response.Write(xml.childNodes(1).text)

    This writes the ENTIRE XML data to the screen. I want to get the above data
    ONLY.

    I want to be able to do this ALL in ASP server side ONLY.

    Any help would be apprciated.

    Thank You.
    B, Jan 13, 2006
    #1
    1. Advertising

  2. B wrote:
    > I have an XML string
    >
    > <?xml version="1.0" encoding="UTF-8"?>
    > <SSOUser><Status>FAIL</Status><Message>Unable to find session id of
    > 1137018716939</Message></SSOUser>
    >
    >
    > That I am trying to retrieve the values between the tags for.
    > I want to get the value from
    > <Status>FAIL</Status>
    > so I get the value "FAIL"
    >
    > I am using the following code (and have tried MANY variations) to
    > retrieve the specific field values but I can not find out how to do
    > this.
    >
    >
    > response.Write(xml.childNodes(1).text)
    >
    > This writes the ENTIRE XML data to the screen. I want to get the
    > above data ONLY.
    >
    > I want to be able to do this ALL in ASP server side ONLY.
    >
    > Any help would be apprciated.
    >
    > Thank You.


    response.write xml.selectSingleNode("//Status").text

    --
    Microsoft MVP -- ASP/ASP.NET
    Please reply to the newsgroup. The email account listed in my From
    header is my spam trap, so I don't check it very often. You will get a
    quicker response by posting to the newsgroup.
    Bob Barrows [MVP], Jan 13, 2006
    #2
    1. Advertising

  3. Assuming the xml variable is the document element then childNodes(1) refers
    to the Message element.

    Given the XML loaded in to DOM variable called oDOM you need:-

    oDOM.selectSingleNode("/SSOUser/Status").text



    "B" wrote:

    > I have an XML string
    >
    > <?xml version="1.0" encoding="UTF-8"?>
    > <SSOUser><Status>FAIL</Status><Message>Unable to find session id of
    > 1137018716939</Message></SSOUser>
    >
    >
    > That I am trying to retrieve the values between the tags for.
    > I want to get the value from
    > <Status>FAIL</Status>
    > so I get the value "FAIL"
    >
    > I am using the following code (and have tried MANY variations) to retrieve
    > the specific field values but I can not find out how to do this.
    >
    >
    > response.Write(xml.childNodes(1).text)
    >
    > This writes the ENTIRE XML data to the screen. I want to get the above data
    > ONLY.
    >
    > I want to be able to do this ALL in ASP server side ONLY.
    >
    > Any help would be apprciated.
    >
    > Thank You.
    >
    >
    >
    AnthonyWJones, Jan 13, 2006
    #3
  4. B

    B Guest

    I am unsure what the oDOM variable is or if I need to set anything to it

    I am laoding the xml data like this:

    Dim xml
    Set xml = Server.CreateObject("Microsoft.XMLDOM")
    xml.async = False
    xml.loadXML(xmlData)

    where xmlData is a variable that is filled with the data from an
    Server.CreateObject("Msxml2.XMLHTTP.3.0")
    get method.

    I tried the below from the previous post.
    response.write xml.selectSingleNode("//Status").text
    and I get an error:
    Object required: '[object]'
    on that line of code.

    Can you please tell me what I need to do/set to get the oDom variable
    populated for your suggestion to work?





    "AnthonyWJones" wrote:

    > Assuming the xml variable is the document element then childNodes(1) refers
    > to the Message element.
    >
    > Given the XML loaded in to DOM variable called oDOM you need:-
    >
    > oDOM.selectSingleNode("/SSOUser/Status").text
    >
    >
    >
    > "B" wrote:
    >
    > > I have an XML string
    > >
    > > <?xml version="1.0" encoding="UTF-8"?>
    > > <SSOUser><Status>FAIL</Status><Message>Unable to find session id of
    > > 1137018716939</Message></SSOUser>
    > >
    > >
    > > That I am trying to retrieve the values between the tags for.
    > > I want to get the value from
    > > <Status>FAIL</Status>
    > > so I get the value "FAIL"
    > >
    > > I am using the following code (and have tried MANY variations) to retrieve
    > > the specific field values but I can not find out how to do this.
    > >
    > >
    > > response.Write(xml.childNodes(1).text)
    > >
    > > This writes the ENTIRE XML data to the screen. I want to get the above data
    > > ONLY.
    > >
    > > I want to be able to do this ALL in ASP server side ONLY.
    > >
    > > Any help would be apprciated.
    > >
    > > Thank You.
    > >
    > >
    > >
    B, Jan 13, 2006
    #4
  5. B wrote:
    > I am unsure what the oDOM variable is or if I need to set anything to
    > it


    He's talking about an xml domdocument, which is what your code is creating.
    >
    > I am laoding the xml data like this:
    >
    > Dim xml
    > Set xml = Server.CreateObject("Microsoft.XMLDOM")
    > xml.async = False
    > xml.loadXML(xmlData)
    >
    > where xmlData is a variable that is filled with the data from an
    > Server.CreateObject("Msxml2.XMLHTTP.3.0")
    > get method.
    >
    > I tried the below from the previous post.
    > response.write xml.selectSingleNode("//Status").text
    > and I get an error:
    > Object required: '[object]'
    > on that line of code.
    >
    > Can you please tell me what I need to do/set to get the oDom variable
    > populated for your suggestion to work?
    >


    Since this is my code, you should have replied to me. Since that line
    failed, that means your xml is not as you represented it. You must remember
    that xml is case sensitive, so if your xml contains <status></status> and I
    search for //Status, I will not find anything.

    A more error-proof way of doing this is:

    set node = xml.selectSingleNode("/SSOUser/Status")
    if isnothing(node) then
    response.write "Could not find ""/SSOUser/Status"""
    else
    response.write node.text
    end if

    Bob Barrows
    --
    Microsoft MVP -- ASP/ASP.NET
    Please reply to the newsgroup. The email account listed in my From
    header is my spam trap, so I don't check it very often. You will get a
    quicker response by posting to the newsgroup.
    Bob Barrows [MVP], Jan 13, 2006
    #5
  6. Bob Barrows [MVP] wrote:
    > A more error-proof way of doing this is:
    >
    > set node = xml.selectSingleNode("/SSOUser/Status")
    > if isnothing(node) then
    > response.write "Could not find ""/SSOUser/Status"""
    > else
    > response.write node.text
    > end if
    >

    Actually, this will work better:

    set node = nothing
    set node = xml.selectSingleNode("/SSOUser/Status")
    if isnothing(node) then
    response.write "Could not find ""/SSOUser/Status"""
    else
    response.write node.text
    end if

    --
    Microsoft MVP -- ASP/ASP.NET
    Please reply to the newsgroup. The email account listed in my From
    header is my spam trap, so I don't check it very often. You will get a
    quicker response by posting to the newsgroup.
    Bob Barrows [MVP], Jan 13, 2006
    #6
  7. B

    B Guest

    Hello,

    I appreciate the help but I am still unable to get this to work.

    My exact XML string is (being writtin to the screen code using
    responce.write):

    <?xml version="1.0" encoding="UTF-8"?>
    <SSOUser><Status>FAIL</Status><Message>Unable to find session id of
    1137018716939</Message></SSOUser>


    The IsNothing() code does not work for me, I get a "Type Mismatch" error on
    this.

    But if I take out that check and just try to write the Node.text to the
    screen it still does not work. I would appareciate your help some more in
    this.

    My exact code I am using (minus the URL where I am getting the XML data from
    (cant post as security reasons)) is below between the 2 lines.

    ________________________________________________________________


    Set xml = Server.CreateObject("Msxml2.XMLHTTP.3.0")

    xml.Open "GET", "URL HERE", False,"ID","PW"
    xml.Send
    Dim xmlData
    xmlData = xml.responseText

    %>

    <br>
    <%Response.Write xmlData %>
    <br><br>

    <%


    Dim xml
    Set xml = Server.CreateObject("Microsoft.XMLDOM")
    xml.async = False
    xml.loadXML(xmlData)

    set node = nothing
    set node = xml.selectSingleNode("/SSOUser/Status")
    if IsNothing(node) then
    'if isblank(node) then
    response.Write("NOT FOUND")
    else
    response.write(node.text)
    end if
    ________________________________________________________________



    "Bob Barrows [MVP]" wrote:

    > Bob Barrows [MVP] wrote:
    > > A more error-proof way of doing this is:
    > >
    > > set node = xml.selectSingleNode("/SSOUser/Status")
    > > if isnothing(node) then
    > > response.write "Could not find ""/SSOUser/Status"""
    > > else
    > > response.write node.text
    > > end if
    > >

    > Actually, this will work better:
    >
    > set node = nothing
    > set node = xml.selectSingleNode("/SSOUser/Status")
    > if isnothing(node) then
    > response.write "Could not find ""/SSOUser/Status"""
    > else
    > response.write node.text
    > end if
    >
    > --
    > Microsoft MVP -- ASP/ASP.NET
    > Please reply to the newsgroup. The email account listed in my From
    > header is my spam trap, so I don't check it very often. You will get a
    > quicker response by posting to the newsgroup.
    >
    >
    >
    B, Jan 16, 2006
    #7
  8. B wrote:
    > Hello,
    >
    > I appreciate the help but I am still unable to get this to work.
    >
    > My exact XML string is (being writtin to the screen code using
    > responce.write):
    >
    > <?xml version="1.0" encoding="UTF-8"?>
    > <SSOUser><Status>FAIL</Status><Message>Unable to find session id of
    > 1137018716939</Message></SSOUser>
    >
    >
    > The IsNothing() code does not work for me, I get a "Type Mismatch"
    > error on this.


    I should have used "Is Nothing" - sorry about that. This code is tested and
    works as desired:

    <%
    dim xmlstring, xmldoc,node
    xmlstring="<?xml version=""1.0"" encoding=""UTF-8""?>" & _
    "<SSOUser><Status>FAIL</Status><Message>Unable to find " & _
    "session id of 1137018716939</Message></SSOUser>"
    set xmldoc=CreateObject("msxml2.domdocument")
    xmldoc.loadXML xmlstring
    set node = nothing
    set node = xmldoc.selectSingleNode("/SSOUser/status")
    if node is nothing then
    response.write "Could not find ""/SSOUser/status"""
    else
    response.write node.text
    end if

    %>

    Bob Barrows
    --
    Microsoft MVP -- ASP/ASP.NET
    Please reply to the newsgroup. The email account listed in my From
    header is my spam trap, so I don't check it very often. You will get a
    quicker response by posting to the newsgroup.
    Bob Barrows [MVP], Jan 16, 2006
    #8
  9. B

    B Guest

    That worked! THANKS!!

    Of cource now the data they are sending to me thought the XML get method is
    formatted wrong, it is returning to XML definitions so it wont work till they
    fix that part, but it works with the hard coded XML data.

    THANKS AGAIN

    "Bob Barrows [MVP]" wrote:

    > B wrote:
    > > Hello,
    > >
    > > I appreciate the help but I am still unable to get this to work.
    > >
    > > My exact XML string is (being writtin to the screen code using
    > > responce.write):
    > >
    > > <?xml version="1.0" encoding="UTF-8"?>
    > > <SSOUser><Status>FAIL</Status><Message>Unable to find session id of
    > > 1137018716939</Message></SSOUser>
    > >
    > >
    > > The IsNothing() code does not work for me, I get a "Type Mismatch"
    > > error on this.

    >
    > I should have used "Is Nothing" - sorry about that. This code is tested and
    > works as desired:
    >
    > <%
    > dim xmlstring, xmldoc,node
    > xmlstring="<?xml version=""1.0"" encoding=""UTF-8""?>" & _
    > "<SSOUser><Status>FAIL</Status><Message>Unable to find " & _
    > "session id of 1137018716939</Message></SSOUser>"
    > set xmldoc=CreateObject("msxml2.domdocument")
    > xmldoc.loadXML xmlstring
    > set node = nothing
    > set node = xmldoc.selectSingleNode("/SSOUser/status")
    > if node is nothing then
    > response.write "Could not find ""/SSOUser/status"""
    > else
    > response.write node.text
    > end if
    >
    > %>
    >
    > Bob Barrows
    > --
    > Microsoft MVP -- ASP/ASP.NET
    > Please reply to the newsgroup. The email account listed in my From
    > header is my spam trap, so I don't check it very often. You will get a
    > quicker response by posting to the newsgroup.
    >
    >
    >
    B, Jan 16, 2006
    #9
  10. B

    dNagel Guest

    <?xml version="1.0" encoding="UTF-8"?>
    <SSOUser><Status>FAIL</Status><Message>Unable to find session id of
    1137018716939</Message></SSOUser>

    That is your exact string right?

    you were writing it with

    <%Response.Write xmlData %>

    and notice that this is what you have working when it's hard coded...

    xmlstring="<?xml version=""1.0"" encoding=""UTF-8""?>" & _
    "<SSOUser><Status>FAIL</Status><Message>Unable to find " & _
    "session id of 1137018716939</Message></SSOUser>"

    This suggests that you need to escape all double quotes in your source string
    before using it as your XML source. You do that by doing a Replace on all
    """" with """""" this is replacing " with "".

    <br>
    <%Response.Write Replace(xmlData, """", """""") %>
    <br><br>

    ....never know... I didn't test this idea, but, it's a quick fix if
    that's really the problem

    hth,

    D.


    B wrote:
    > That worked! THANKS!!
    >
    > Of cource now the data they are sending to me thought the XML get method is
    > formatted wrong, it is returning to XML definitions so it wont work till they
    > fix that part, but it works with the hard coded XML data.
    >
    > THANKS AGAIN
    >
    > "Bob Barrows [MVP]" wrote:
    >
    >
    >>B wrote:
    >>
    >>>Hello,
    >>>
    >>>I appreciate the help but I am still unable to get this to work.
    >>>
    >>>My exact XML string is (being writtin to the screen code using
    >>>responce.write):
    >>>
    >>><?xml version="1.0" encoding="UTF-8"?>
    >>><SSOUser><Status>FAIL</Status><Message>Unable to find session id of
    >>>1137018716939</Message></SSOUser>
    >>>
    >>>
    >>>The IsNothing() code does not work for me, I get a "Type Mismatch"
    >>>error on this.

    >>
    >>I should have used "Is Nothing" - sorry about that. This code is tested and
    >>works as desired:
    >>
    >><%
    >>dim xmlstring, xmldoc,node
    >>xmlstring="<?xml version=""1.0"" encoding=""UTF-8""?>" & _
    >>"<SSOUser><Status>FAIL</Status><Message>Unable to find " & _
    >>"session id of 1137018716939</Message></SSOUser>"
    >>set xmldoc=CreateObject("msxml2.domdocument")
    >>xmldoc.loadXML xmlstring
    >>set node = nothing
    >>set node = xmldoc.selectSingleNode("/SSOUser/status")
    >>if node is nothing then
    >> response.write "Could not find ""/SSOUser/status"""
    >>else
    >> response.write node.text
    >>end if
    >>
    >>%>
    >>
    >>Bob Barrows
    >>--
    >>Microsoft MVP -- ASP/ASP.NET
    >>Please reply to the newsgroup. The email account listed in my From
    >>header is my spam trap, so I don't check it very often. You will get a
    >>quicker response by posting to the newsgroup.
    >>
    >>
    >>
    dNagel, Jan 16, 2006
    #10
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Lamberti Fabrizio
    Replies:
    0
    Views:
    618
    Lamberti Fabrizio
    Jun 23, 2004
  2. Eduardo Rosa

    Microsoft.XMLDom

    Eduardo Rosa, Jul 2, 2004, in forum: ASP General
    Replies:
    1
    Views:
    103
    Martin Honnen
    Jul 2, 2004
  3. jfizer
    Replies:
    2
    Views:
    144
    jfizer
    Jul 30, 2004
  4. Eitan
    Replies:
    1
    Views:
    94
    Martin Honnen
    Nov 28, 2004
  5. Replies:
    36
    Views:
    354
    Dr John Stockton
    Dec 6, 2005
Loading...

Share This Page