Link to home
Start Free TrialLog in
Avatar of SweatCoder
SweatCoderFlag for United States of America

asked on

Microsoft.XMLDOM in asp - get arbitrary nodes

I'm trying to do this in asp, iis5, win2k:

SET objXML = Server.CreateObject("Microsoft.XMLDOM")
objXML.async = False
objXML.load("\\casablanca\Public\changemark.xml")
'objXML.loadXML(BinaryBuffer) 'can i load a binary xml stream directly into the xml object?
                  
SET Node = objXML.documentElement.selectSingleNode("Text/TextLine")
IF LEN (Node.Text) > 0 THEN
     Response.Write " " & Node.Text & " "
END IF
SET Node = NOTHING : SET objXML = NOTHING

I have 3 problems:

(1) the code breaks on the "SET Node" line with "Object required: 'objXML.documentElement'"
(2) the snippet i'm using, if it did work, doesn't do quite what i want. i just found the snippet online. I need to be able to parse an xml document and extract the text/values from any and all occurrances of <TextLine>.
(3) which version of the xmldom dll should i use? i know there are a few. it must work "out of the box" on win2k, xp and win2003. i'm using what i think is the oldest version to ensure that it works on vanilla win2k os. But maybe there's a newer version that comes with win2k.

. . .my xml file looks like this:

 <?xml version="1.0" encoding="UTF-8" ?>
- <IGCMarkupDocument xmlns="http://www.somedomain.com" revision="4" majorversion="0" minorversion="0">
- <PageList pagecount="1">
- <Page index="0">
- <AuthorList authorcount="1">
- <Author name="johne">
- <Text id="0" time="1100715019" color="255,0,0" fontsize="1.60284" italic="false" bold="false" opaque="false" underline="false" font="Arial" hyperlink="">
-  <TextLine>text on drawing in markup</TextLine>
  </Text>
- <Changemark id="1" time="1100715030" color="255,0,0" hyperlink="">
  <Title>Description of changemark, for john</Title>
  <Comment>This is the details of the changemarks for Johns Changemark</Comment>
- <Viewstate Extents="true">
  <Page>0</Page>
  <ScaleFactor>1.44467</ScaleFactor>
  <Rotation>0.0</Rotation>
- <EyePoint>
  <x>367.5</x>
  <y>244.0</y>
  </EyePoint>
- <DeviceRect>
  <top>0</top>
  <left>0</left>
  <bottom>706</bottom>
  <right>1157</right>
  </DeviceRect>
- <LayerTable LayerCount="1">
  <LayerBlock>1</LayerBlock>
  </LayerTable>
  </Viewstate>
  </Changemark>
  </Author>
  </AuthorList>
  </Page>
  </PageList>
  </IGCMarkupDocument>

Avatar of Anthony Perkins
Anthony Perkins
Flag of United States of America image

If you can lose the namespace as in:

<?xml version="1.0" encoding="UTF-8" ?>
<!--IGCMarkupDocument xmlns="http://www.somedomain.com" revision="4" majorversion="0" minorversion="0"-->
<IGCMarkupDocument revision="4" majorversion="0" minorversion="0">
      <PageList pagecount="1">
            <Page index="0">
                  <AuthorList authorcount="1">
                        <Author name="johne">
                              <Text id="0" time="1100715019" color="255,0,0" fontsize="1.60284" italic="false" bold="false" opaque="false" underline="false" font="Arial" hyperlink="">  
                                    <TextLine>text on drawing in markup</TextLine>  
                              </Text>
                              <Changemark id="1" time="1100715030" color="255,0,0" hyperlink="">  
                                    <Title>Description of changemark, for john</Title>  
                                    <Comment>This is the details of the changemarks for Johns Changemark</Comment>
                                    <Viewstate Extents="true">  
                                          <Page>0</Page>  
                                          <ScaleFactor>1.44467</ScaleFactor>  
                                          <Rotation>0.0</Rotation>
                                          <EyePoint>  
                                                <x>367.5</x>  
                                                <y>244.0</y>  
                                          </EyePoint>
                                          <DeviceRect>  
                                                <top>0</top>  
                                                <left>0</left>  
                                                <bottom>706</bottom>  
                                                <right>1157</right>  
                                          </DeviceRect>
                                          <LayerTable LayerCount="1">  
                                                <LayerBlock>1</LayerBlock>  
                                          </LayerTable>  
                                    </Viewstate>  
                              </Changemark>  
                        </Author>  
                  </AuthorList>  
            </Page>  
      </PageList>  
</IGCMarkupDocument>

Than it is as simple as this:

Dim objXml
Dim Nodes
Dim Node

Set objXml = Server.CreateObject("Microsoft.XMLDOM")
objXml.async = False
If objXml.Load("\\casablanca\Public\changemark.xml") Then
   Set Nodes = objXml.selectNodes("IGCMarkupDocument/PageList/Page/AuthorList/Author/Text/TextLine")
   For Each Node In Nodes
           Response.Write "&nbsp;" & Node.Text & "&nbsp;"
   Next
   Set Nodes = Nothing
Else
           Response.Write objXml.parseError.reason
End If
Set objXml = Nothing

If you cannot, than it becomes somewhat more difficult ...
Avatar of SweatCoder

ASKER

thanks for the response. yes, i can lose the namespace if needed. but from your syntax it appears that i can only retrieve the value for a specific instance of TextLine, with a specifc heirarchy. There may be many instances throughout the document, even though this particular xml doc only has one.

how can i get all instances? is there a way to get all instances of any arbitrary element regardless of its tree depth or location? for example, if i wanted to extract the text of all <FOO></FOO> elements, regardless of position, heirarchy, or whatever, is there a command that will do that? that's really what i'm looking for, because i'm wanting to get more than just <TextLine>. . .i used that as my only example for simplicity.

THANKS.
ASKER CERTIFIED SOLUTION
Avatar of Anthony Perkins
Anthony Perkins
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
you're beautiful! works great.
here's the final working implementation, in case it might help somebody else:

SET objXML = Server.CreateObject("Msxml2.DomDocument")
objXml.async = False
PathToXML = Server.MapPath("./changemark.xml")
writeme "PathToXML: " & PathToXML
If objXml.Load(PathToXML) Then
      'Set Nodes = objXml.getElementsByTagName("TextLine")
      Set Nodes = objXml.selectNodes("//TextLine")
      For Each Node In Nodes
            Response.Write "&nbsp;" & Node.Text & "&nbsp;"
      Next
      Set Nodes = Nothing
Else
      Response.Write "ERROR: " & objXml.parseError.reason
End If
Set objXml = Nothing