none
Using XMLReader in a VB Script RRS feed

  • Question

  • Hi,

    The code below works well for me in VB.NET, however, I need to perform this using VBScript. I have been unable to find any documentation or examples of how to create an object that references the XMLTextReader. All I can find is examples using the XMLDOM object. Any suggestions would be appreciated.

    Thanks

    Dim reader As XmlTextReader = New XmlTextReader("F:\GM HFV6 Daily Events\9-26-2014\HFV6 Block Main.xml")

            'How to get a point
            Do While (reader.Read())
                If reader.MoveToContent() = XmlNodeType.Element And reader.Name = "point" Then
                    If reader.HasAttributes Then 'If attributes exist
                        While reader.MoveToNextAttribute()
                            'Display attribute name and value.
                            Console.Write(" {0}='{1}'", reader.Name, reader.Value)
                        End While
                    End If
                    Console.WriteLine()
                End If
            Loop

    Thursday, October 2, 2014 2:16 PM

Answers

  • You still have to design a program to match your process.  You have all of the pieces.  Start by deciding what the data hierarchy looks like and extract the date into objects that can be sued to drive your process.  YOU will have to provide the design as we cannot know what it is you need and this forum is not really for more than answering specific questions.

    Remember - you have to have a design.  It is an engineering challenge to crate a design that satisfies your criteria.  Writing the coed is the simple part but can't be done without a deign.


    ¯\_(ツ)_/¯

    Tuesday, October 7, 2014 9:04 PM

All replies

  • That is it.  You cannot use VB.NEt code in VBScript.  You can, however, use VB.Net code in PowerShell with some adjustments.


    ¯\_(ツ)_/¯

    Thursday, October 2, 2014 6:22 PM
  • The code below works well for me in VB.NET, however, I need to perform this using VBScript.

    Why?


    -- Bill Stewart [Bill_Stewart]

    Thursday, October 2, 2014 6:28 PM
    Moderator
  • The XML objects in VBScript can do what you are doing in VB in only about three lines.  I cannot understand why you would need to use the more primitive XMLText reader.  THe XMLDocument is much more versatile and easier to use.

     


    ¯\_(ツ)_/¯

    Thursday, October 2, 2014 6:41 PM
  • Because I am using a CAD product that uses VB Script as a programming tool and I need to read an XML file, rather than write my own parser was trying to learn the XML methods out there and XMLTextReader did the job for me. Looks like I'll just have to learn how to do it use the Document Model.
    Friday, October 3, 2014 1:48 PM
  • Looks like I'll have to learn how to do it use the DOM object instead then! Thanks.
    Friday, October 3, 2014 1:49 PM
  • The DOM object is easier than the reader easer with XPAth to drive the reader.  The reader is used to build tools and not really intended for parsing.

    Perhaps you will tell us what you need to do rather than what you have donethat does not work.

    It looks likeyou want a node or node set.

    Set nodes = xmlDom.SelectNodes("//point")
    for each node in nodes
       ' do something with node attributes
    next


    ¯\_(ツ)_/¯

    Friday, October 3, 2014 2:02 PM
  • The XML below is typical of a what I get for a measurement feature. I need to extract it's name (create a CAD layer), then draw the nominal circle, actual circle and touch points, once I have the xyz I create an array and then use a built-in function to draw the point in the CAD system, e.g. CAD.AddPoint(arrPoint).

    <feature id="FEAT_Datum_RC_raw" name="Datum_RC_raw" common_space="YES">
       <circle_feature type="INNER">
        <circle_feature_nominal reporting_transform="XFORM_NOM_RA-RB-RC_Rough_Align">
         <center_point x="-346.000000" y="-45.000000" z="4.000000"/>
         <normal i="0.000000" j="0.000000" k="-1.000000"/>
         <diameter>14.012500</diameter>
        </circle_feature_nominal>
        <circle_feature_actual reporting_transform="XFORM_ACT_RA-RB-RC_Rough_Align">
         <center_point x="-345.984474" y="-44.998016" z="3.996796"/>
         <normal i="-0.000028" j="0.000082" k="-1.000000"/>
         <diameter>14.012679</diameter>
        </circle_feature_actual>
       </circle_feature>
       <point_list>
        <point_data id="TOUCH_1_Datum_RC_raw">
         <measured_point>
          <point x="-350.057781" y="-40.930992" z="4.060752"/>
          <normal i="0.758250" j="-0.590388" k="0.276584"/>
          <ball_radius>1.251170</ball_radius>
         </measured_point>
        </point_data>
        <point_data id="TOUCH_2_Datum_RC_raw">
         <measured_point>
          <point x="-350.029639" y="-40.902973" z="4.059664"/>
          <normal i="0.753523" j="-0.592812" k="0.284213"/>
          <ball_radius>1.251170</ball_radius>
         </measured_point>
        </point_data>
        <point_data id="TOUCH_3_Datum_RC_raw">
         <measured_point>
          <point x="-349.999657" y="-40.875501" z="4.058977"/>
          <normal i="0.758183" j="-0.588114" k="0.281569"/>
          <ball_radius>1.251170</ball_radius>
         </measured_point>
        </point_data>
    </point_list>
    </feature>

    Friday, October 3, 2014 5:05 PM
  • You can extract any element or attribute directly with XPath.

    You mention many things that do not in any way translate to the identifiers in the XML.  You must use identifiers.

    I see not attribute called "name".  What is it you are calling "name" - You posted: "I need to extract it's name ..."


    ¯\_(ツ)_/¯

    Friday, October 3, 2014 6:59 PM
  • This is closer to what your XML actually looks like.

    <?xml version="1.0"?>
    <feature id="FEAT_Datum_RC_raw" name="Datum_RC_raw" common_space="YES">
        <circle_feature type="INNER">
            <circle_feature_nominal reporting_transform="XFORM_NOM_RA-RB-RC_Rough_Align">
                <center_point x="-346.000000" y="-45.000000" z="4.000000"/>
                <normal i="0.000000" j="0.000000" k="-1.000000"/>
                <diameter>14.012500</diameter>
            </circle_feature_nominal>
            <circle_feature_actual reporting_transform="XFORM_ACT_RA-RB-RC_Rough_Align">
                <center_point x="-345.984474" y="-44.998016" z="3.996796"/>
                <normal i="-0.000028" j="0.000082" k="-1.000000"/>
                <diameter>14.012679</diameter>
            </circle_feature_actual>
        </circle_feature>
        <point_list>
            <point_data id="TOUCH_1_Datum_RC_raw">
                <measured_point>
                    <point x="-350.057781" y="-40.930992" z="4.060752"/>
                    <normal i="0.758250" j="-0.590388" k="0.276584"/>
                    <ball_radius>1.251170</ball_radius>
                </measured_point>
            </point_data>
            <point_data id="TOUCH_2_Datum_RC_raw">
                <measured_point>
                    <point x="-350.029639" y="-40.902973" z="4.059664"/>
                    <normal i="0.753523" j="-0.592812" k="0.284213"/>
                    <ball_radius>1.251170</ball_radius>
                </measured_point>
            </point_data>
            <point_data id="TOUCH_3_Datum_RC_raw">
                <measured_point>
                    <point x="-349.999657" y="-40.875501" z="4.058977"/>
                    <normal i="0.758183" j="-0.588114" k="0.281569"/>
                    <ball_radius>1.251170</ball_radius>
                </measured_point>
            </point_data>
        </point_list>
    </feature>
    

    To get a circle we would query for  a circle thing.

    The result would then be extractable as values of elements.

    Set circle xmlDom.SelectSingleNode("//circle_feature")
    type=circle.Type


    ¯\_(ツ)_/¯


    • Edited by jrv Friday, October 3, 2014 7:07 PM
    Friday, October 3, 2014 7:02 PM
  • Here is an example of how easy it can be:

    Set xmlDoc = CreateObject("MSXML.DOMDocument")
    xmlDoc.Async = False
    x = xmlDoc.Load("c:\temp3\draw.xml")
    Set c = xmlDoc.SelectSingleNode("//circle_feature")
    WScript.Echo c.NodeName
    For Each attr In c.attributes
        WScript.Echo attr.nodeName, attr.nodeValue
    Next
    
    Set n = c.SelectSingleNode("//center_point")
    For Each a In n.attributes
        WScript.Echo vbTab,a.NodeName, a.nodeValue
    Next


    ¯\_(ツ)_/¯

    Friday, October 3, 2014 7:29 PM
  • Thanks for the reply, much appreciated, I was able to use the code above and successfully read a node and get some attributes. The next problem is that the XML file is a repeating pattern, i.e. in the example given earlier <feature_id will be repeated many times:

    <feature id="FEAT_Circle1" name="Circle1" common_space="YES">
    .
    .
    <feature>
    <feature id="FEAT_Circle2" name="Circle2" common_space="YES">
    .
    .
    .
    <feature>

    etc, etc.

    Do I have use SelectNodes and then work my way through the list of nodes?


     

    Tuesday, October 7, 2014 7:03 PM
  • You still have to design a program to match your process.  You have all of the pieces.  Start by deciding what the data hierarchy looks like and extract the date into objects that can be sued to drive your process.  YOU will have to provide the design as we cannot know what it is you need and this forum is not really for more than answering specific questions.

    Remember - you have to have a design.  It is an engineering challenge to crate a design that satisfies your criteria.  Writing the coed is the simple part but can't be done without a deign.


    ¯\_(ツ)_/¯

    Tuesday, October 7, 2014 9:04 PM