We help IT Professionals succeed at work.

Parsing an XML File???

nesifa
nesifa asked
on
Hi everyone,

  I have an XML File which I will paste at the end of this Description.
I am mostly interested to read everything within a block tag. Any idea how to read it recursively.The code should be on ASP...

Thanks Nesifa

This is the file******************
<?xml version="1.0"?>
<content>
      <globalProperties>
            <externalMetadata>
                  <source>ADL</source>
                  <model>ADL SCORM 1.1</model>
                  <location><![CDATA[coursemetadata.xml]]></location>
            </externalMetadata>
            <curricularTaxonomy>
                  <model>taxonomy1</model>
            </curricularTaxonomy>
      </globalProperties>
      <block id="pu20011019135311403">
            <identification>
                  <title>Parce csf scorm1.1</title>
                  <description>a general description</description>
                  <labels>
                        <curricular>Course</curricular>
                        <developer>NYUOnline, Inc.</developer>
                  </labels>
            </identification>
            <block id="bl20011019135451687">
                  <externalMetadata>
                        <source>ADL</source>
                        <model>ADL SCORM 1.1</model>
                        <location><![CDATA[bl20011019135451687\bl20011019135451687.xml]]></location>
                  </externalMetadata>
                  <identification>
                        <title>block1Level1</title>
                        <description>description</description>
                        <labels>
                              <curricular>NYUOnline, Inc.</curricular>
                              <developer>1</developer>
                        </labels>
                  </identification>
            <block id="bl20011019135512233">
                  <externalMetadata>
                        <source>ADL</source>
                        <model>ADL SCORM 1.1</model>
                        <location><![CDATA[bl20011019135451687\bl20011019135512233\bl20011019135512233.xml]]></location>
                  </externalMetadata>
                  <identification>
                        <title>block level2</title>
                        <description>block decription</description>
                        <labels>
                              <curricular>NYUOnline, Inc.</curricular>
                              <developer>second level</developer>
                        </labels>
                  </identification>
                  <sco id="ob20011019135543000">
                        <externalMetadata>
                              <source>ADL</source>
                              <model>ADL SCORM 1.1</model>
                              <location><![CDATA[bl20011019135451687\bl20011019135512233\ob20011019135543000\ob20011019135543000.xml]]></location>
                        </externalMetadata>
                        <identification>
                              <title>object1</title>
                              <description>description</description>
                              <labels>
                                    <curricular>Object</curricular>
                                    <developer>NYUOnline, Inc.</developer>
                              </labels>
                        </identification>
                        <launch>
                              <location><![CDATA[bl20011019135451687\bl20011019135512233\ob20011019135543000\index.htm]]></location>
                        </launch>
                  </sco>
                  <sco id="ob20011019141746450">
                        <externalMetadata>
                              <source>ADL</source>
                              <model>ADL SCORM 1.1</model>
                              <location><![CDATA[bl20011019135451687\bl20011019135512233\ob20011019141746450\ob20011019141746450.xml]]></location>
                        </externalMetadata>
                        <identification>
                              <title>object2</title>
                              <description>descr obj</description>
                              <labels>
                                    <curricular>Object</curricular>
                                    <developer>NYUOnline, Inc.</developer>
                              </labels>
                        </identification>
                        <launch>
                              <location><![CDATA[bl20011019135451687\bl20011019135512233\ob20011019141746450\index.htm]]></location>
                        </launch>
                  </sco>
            </block>
            <block id="bl20011019141915670">
                  <externalMetadata>
                        <source>ADL</source>
                        <model>ADL SCORM 1.1</model>
                        <location><![CDATA[bl20011019135451687\bl20011019141915670\bl20011019141915670.xml]]></location>
                  </externalMetadata>
                  <identification>
                        <title>block level2</title>
                        <description>dfssdf</description>
                        <labels>
                              <curricular>NYUOnline, Inc.</curricular>
                              <developer>second level</developer>
                        </labels>
                  </identification>
                  <sco id="ob20011019141927530">
                        <externalMetadata>
                              <source>ADL</source>
                              <model>ADL SCORM 1.1</model>
                              <location><![CDATA[bl20011019135451687\bl20011019141915670\ob20011019141927530\ob20011019141927530.xml]]></location>
                        </externalMetadata>
                        <identification>
                              <title>sdfsfsdf</title>
                              <description>sdfsdfsdf</description>
                              <labels>
                                    <curricular>Object</curricular>
                                    <developer>NYUOnline, Inc.</developer>
                              </labels>
                        </identification>
                        <launch>
                              <location><![CDATA[bl20011019135451687\bl20011019141915670\ob20011019141927530\index.htm]]></location>
                        </launch>
                  </sco>
            </block>
            </block>
      </block>
</content>
Comment
Watch Question

Commented:
This uses Microsoft XML Parser 3.0

Set xmlDoc = CreateObject("MSXML2.DOMDocument.3.0")

Set blockElems = xmlDoc.getElementsByTagName("block")

' now just loop through them

if blockElems.length > 0 then
     For counter = 0 To blockElem.length - 1
         set blockElem = blockElems.Item(counter)
     Next
End if

Author

Commented:
Hi Midavis,
  I did this as well.
  I am interested to get whatever tags are either in betweenblock tags, save that information in database,
Then go to next block... then to sco......
Can you be some more specific in doing this.

Thanks
Nesifa

Commented:
hi nesifa,
  Use XMLDOM to Read the XML File...

ASP Sample Example

<html>
<body>
<script type="text/vbscript">

txt="<h1>Traversing the node tree</h1>"
document.write(txt)

set xmlDoc=CreateObject("Microsoft.XMLDOM")
xmlDoc.async="false"
xmlDoc.load("note.xml")

for each x in xmlDoc.documentElement.childNodes
  document.write("<b>" & x.nodename & "</b>")
  document.write(": ")
  document.write(x.text)
  document.write("<br>")
next

</script>
</body>
</html>

For more clarification You can refer http://www.w3schools.com/dom/dom_examples.asp

Regards
 Pack
Why are your BLOCK tags nested within each other?

Is this an error, or is this how it should be?

It may be easier to have your blocks in this format:

<block />
<block />
<block />

as apposed to:

<block>
    <block>
        <block>
        </block>
    </block>
</block>

Then you could just do a "For each block in content" (I know it's not that simple, but you get the idea?)

Commented:
Sorry I did not comment earlier on your question.  To get tag elements from inside the current block you basically do the same thing.

Set xmlDoc = CreateObject("MSXML2.DOMDocument.3.0")

Set blockElems = xmlDoc.getElementsByTagName("block")

' now just loop through them

if blockElems.length > 0 then
    For counter = 0 To blockElem.length - 1
        set blockElem = blockElems.Item(counter)
        set externalMetadataElems = blockElem.getElementsByTagName("externalMetadata")
       
        for counter2 = 0 to externalMetadataElems.length - 1
            set externalMetadataElem = externalMetadataElems.Item(counter2)

            strSource = GetChildeElementText(externalMetadataElem, "source")
        next
    Next
End if

The above code has a function I like to use to grab text from a child elem.  It seems to be useful.

' this helper function will take in an XML Element and child name, and will return
' the text in the child element with the given name.
Private Function GetChildElementText(ByVal sElem As Object, sChildName As String) As String
    On Error Resume Next

    GetChildElementText = ""
   
    GetChildElementText = sElem.getElementsByTagName(sChildName).Item(0).text
   
End Function


For the first use of the function source would equal "ADL".  Hope this helps.

BTW, I also noticed that the xml seemed a bit confusing to read.  There were blocks inside blocks and externalMetadata tags inside other blocks and sco's.  You may want to clean that up.

Explore More ContentExplore courses, solutions, and other research materials related to this topic.