Forum Discussion

Divya_anilkumar's avatar
12 years ago

Reading sub child of a sub child in an xml file

Hi..

I need to read the values of a child/sub node of a child node. Pls see the xml file attached.. There may be multiple sub nodes of the same name, i need all the values read and returned in an array..

I was using this code to read a child node  -



Function GetXMLChildNodeValue(XMLFile,NodeName,SubNodeName)

Dim RtnValue

Dim XMLDocObj

Dim ChildNodesObject

Dim ChildNode

Dim Reason,ChildSubNodes



  LogFolder = Log.AppendFolder("Getting Node value for SubNodeName :" & SubNodeName & _

  " in XML file: " & XMLFile)

  RtnValue = TC_FAIL

 

  Set XMLDocObj = CreateObject("Microsoft.XMLDOM")

  XMLDocObj.async = False

  XMLDocObj.Load(XMLFile)

 

  If XMLDocObj.parseError.errorCode = 0 Then

    If XMLDocObj.documentElement.hasChildNodes then

      Set ChildNodesObject = XMLDocObj.documentElement.childNodes                                       

      For Each ChildNode In ChildNodesObject

        If ChildNode.nodeName = NodeName then

          If ChildNode.hasChildNodes then

            For each ChildSubNodes in ChildNode.childNodes

              If ChildSubNodes.nodeName = SubNodeName then

                RtnValue = ChildSubNodes.Text

                Log.Message("Value of the " & SubNodeName & " Node is " & RtnValue)

              End If

            Next

          Else

            Log.Message("There are no sub child nodes")

          End If

        End If           

      Next

    Else

      Log.Message("There are no child nodes")

    End If

  Else

    Log.Error("Cannot parse the xml document;Check whether the file extension is correct")

  End If

 

  GetXMLChildNodeValue = RtnValue

  Log.PopLogFolder()

End Function



This reads all child node values.. I can modify it for them to be returned in an array.

Can this be extended to read sub child node values too? Like for say "Line width" in the attached file?

Also how can i read the values for <size> under <Label Font>.. that would be a child node's child node's child...





2 Replies

  • bbi's avatar
    bbi
    Contributor






    I don't know if that can help but i access to my nodes by xpath when we already know the path used, using the function below.

    Call sample is

     



    /* --------------------------------------------------------------------------- 



    Vérification dans le fichier xml d'export



    --------------------------------------------------------------------------- */



    function CheckExportValue(pIdx, pValue)



    {



    var iRet = 0;



    try {



    var EnvLogName = Query_SQL_ADO("select ENVLOGNAME from FMENV where BATIDX = " + pIdx).Lines[0];



    var XMLFile = _CheminExport + '\\' + EnvLogName + '_' + pIdx + '.XML';



    iRet = CheckXMLNodeValue(XMLFile, "//ENVELOPE_CONTENT/BATCH/CLB_DESC", pValue);



    }



     



    catch(e) {



    LogExceptionByLevel(e, "Error", "CheckExportValue(" + pIdx + ", " + pValue + ") ");



    iRet = 1;



    }



    finally {



    return iRet;



    }



    }







    for an xml having:



    <?xml version="1.0" encoding="UTF-8"?>

    <!--Itesoft.Freemind Enterprise XML file created using FreeMind Data.-->

    <ENVELOPE_CONTENT>

     <VERSION>2.5.3</VERSION>

     <BATCH>

      <IDX>2</IDX>

      <ENTITY_PRIORITY>5</ENTITY_PRIORITY>

      <CLB_COD>DEFAULT_BATCH</CLB_COD>

      <CLB_DESC>Lot par défaut</CLB_DESC>

      <RECEPTION_DATE>2013-02-22</RECEPTION_DATE>

      <PRIORITY>5</PRIORITY>

      <CONFIDENTIALITY></CONFIDENTIALITY>

      <SOURCE_CAPTURE>FILE</SOURCE_CAPTURE>

      <STRUCTURE>VARIABLE</STRUCTURE>

      <REJECT_MESSAGES>

      </REJECT_MESSAGES>

      <INDEXES>

      </INDEXES>

     </BATCH>

     <ENVELOPE>

      <IDX>2</IDX>

      <ENTITY_PRIORITY>5</ENTITY_PRIORITY>

      <CLE_COD>DEFAULT_ENVELOPE</CLE_COD>

      <CLE_DESC>Pli par défaut</CLE_DESC>

      <RECIPIENT></RECIPIENT>

      <COMMENT></COMMENT>

      <REJECT_MESSAGES>

      </REJECT_MESSAGES>

      <INDEXES>

      </INDEXES>

     </ENVELOPE>

     <DOCUMENTS>

      <DOCUMENT>

       <IDX>2</IDX>

       <ENTITY_PRIORITY>5</ENTITY_PRIORITY>

       <CLD_COD>SLESYS_MISC</CLD_COD>

       <CLD_DESC>Divers</CLD_DESC>

       <CATEGORY>OTHER</CATEGORY>

       <TIF_FILENAME>divers_2_si.tif</TIF_FILENAME>

       <COMMENT></COMMENT>

       <REJECT_MESSAGES>

       </REJECT_MESSAGES>

       <INDEXES>

       </INDEXES>

      </DOCUMENT>

     </DOCUMENTS>

    </ENVELOPE_CONTENT>







    The function is here:



    /* ---------------------------------------------------------------------------------------



    Vérifier la valeur d'un noeud XML



    Nécessite:



    MSXML6 présent dans le système.



    Paramètres:



    [String] pXMLFile : Nom complet du fichier XML à vérifier.



    [String] pNode : Requête XPATH de la valeur de noeud à tester.



    [String] pValue : Valeur à tester.



    Retour:



    (Integer) 0 si test ok, 1 si erreur, un log d'erreur est généré dans ce cas.



    ------------------------------------------------------------------------------------ */



    function CheckXMLNodeValue(pXMLFile, pNode, pValue)



    {



    var iRet = 0;



    try {



    // Création objet COM avec MSXML6 et charger le fichier



    var Doc, s;



    Doc = Sys.OleObject("Msxml2.DOMDocument.6.0");



    Doc.async = false;



    Doc.load(pXMLFile);



    // Contrôle validité XML



    if (Doc.parseError.errorCode != 0) {



    s = "Echec ouverture XML" + "\n" +

    "Raison:\t" + Doc.parseError.reason + "\n" +



    "Ligne:\t" + aqConvert.VarToStr(Doc.parseError.line) + "\n" +



    "Pos:\t" + aqConvert.VarToStr(Doc.parseError.linePos) + "\n" +



    "Source:\t" + Doc.parseError.srcText;



    throw new Error(s);



    }



     



    // Accès au noeud par XPath



    var Nodes = Doc.selectNodes(pNode);



    if (Nodes.item(0).text != pValue)



    throw new Error('La valeur "' + Nodes.item(0).text + '" est différente de la valeur attendue "' + pValue + '" dans le fichier "' + pXMLFile + '".')



    else



    Log.Message('La valeur "' + Nodes.item(0).text + '" est bien la valeur attendue "' + pValue + '" dans le fichier "' + pXMLFile + '".')



    }



     



    catch(e) {



    LogExceptionByLevel(e, "Error", "CheckXMLNodeValue(" + pXMLFile + ", " + pNode + ", " + pValue + ")");



    iRet = 1;



    }



    finally {



    return iRet;



    }



    }