5 Replies Latest reply on Dec 7, 2009 9:17 AM by RK...

    how to typecast string to xml Nodelist in java

    sathiyabala

      hai

       

         i need to convert the string to nodelist in java

       

       

      Element parentcustom = (Element)nod;

       

      String resultNodeValue = replaceAngleBracketsChildType(parentcustom.toString());

       

       

      NodeList parentcustomList = (NodeList)resultNodeValue;

       

       

       

      i have done it in above way, but i am not getting the correct answer

       

      i need this        resultNodeValue  to be converted to nodelist...

       

       

      can any one help me

       

       

       

      Thanks in Advance...

        • 1. Re: how to typecast string to xml Nodelist in java
          RK... Level 3

          Your requirement is to get "parentcustomdetails" inside  "parentruledetails" node?.

           

          The following code will do that,

           

          NodeList children = doc.getDocumentElement().getChildNodes();
          for (int i = 0; i < children.getLength(); i++) {
               Node parentRule = children.item(i);
               if (parentRule.getNodeName().equals("Parentrule")) {
                    NodeList nlParentRule = parentRule.getChildNodes();
          
                    for (int j = 0; j < nlParentRule.getLength(); j++) {
                         Node parentRuleDetails = nlParentRule.item(j);
                         if (parentRuleDetails.getNodeName().equals(
                                   "parentruledetails")) {
                              NodeList nlParentRuleDetails = parentRuleDetails
                                        .getChildNodes();
          
                              for (int k = 0; k < nlParentRuleDetails.getLength(); k++) {
                                   Node parentCustomDetails = nlParentRuleDetails
                                             .item(k);
                                   if (parentCustomDetails.getNodeName().equals(
                                             "parentcustomdetails")) {
                                        NamedNodeMap attributes = parentCustomDetails
                                                  .getAttributes();
          
                                        System.out.println(parentCustomDetails
                                                  .getNodeName());
                                        for (int l = 0; l < attributes.getLength(); l++) {
                                             Node attr = attributes.item(l);
                                             System.out.println("\t"
                                                       + attr.getNodeName() + " -- "
                                                       + attr.getNodeValue());
                                        }
                                   }
                              }
                         }
                    }
               }
          }
          

           

          The output of this code is,

           

          parentcustomdetails
               CHILD_TYPE -- Console
               CMD_CODE -- 37135
               CMD_NAME -- login authentication [string]
               CMD_OPTION -- login authentication 
               COLUMN_INDEX -- 0
               CONDN_OPER -- exists
               CONDN_VALUE -- True
               PARENT_RULE_CODE -- PR630
               RULE_TYPE_FLAG -- Customrule
          parentcustomdetails
               CHILD_TYPE -- Console
               CMD_CODE -- 37135
               CMD_NAME -- login authentication [string]
               CMD_OPTION -- string
               COLUMN_INDEX -- 1
               CONDN_OPER -- equalsto
               CONDN_VALUE -- AAAAAAAAA
               PARENT_RULE_CODE -- PR630
               RULE_TYPE_FLAG -- Customrule
          

          • 2. Re: how to typecast string to xml Nodelist in java
            sathiyabala Level 1

            sorry again

             

            the thing u gave is right but, if i get the nodename and nodevalue .. i cant set that value.

             

            i need to get set the attribute and then oly i can save it in db

             

            can u say me that pl

            • 3. Re: how to typecast string to xml Nodelist in java
              sathiyabala Level 1

              sorry again

               

              the thing u gave is right but, if i get the nodename and nodevalue .. i cant set that value.

               

              i need to get set the attribute and then oly i can save it in db

               

              can u say me that pl

               

               

              File file =

               

              new File(pFilePath);

              DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();

              DocumentBuilder db = dbf.newDocumentBuilder();

              Document doc = db.parse(file);

              NodeList children = doc.getDocumentElement().getChildNodes();

               

              for (int i = 0; i < children.getLength(); i++) {

              Node parentRule = children.item(i);

               

              if (parentRule.getNodeName().equals("Parentrule")) {

              NodeList nlParentRule = parentRule.getChildNodes();

              System.

              out.println(":nlParentRule::::::::INSIDE PARENTRULE::::::::::::::");

               

              for (int j = 0; j < nlParentRule.getLength(); j++) {

              Node parentRuleDetails = nlParentRule.item(j);

               

              if (parentRuleDetails.getNodeName().equals("parentruledetails")) {

              System.

              out.println(":nlParentRule::::::::INSIDE PARENTRULEDETAILS::::::::::::::");

              NodeList nlParentRuleDetails = parentRuleDetails.getChildNodes();

               

              for (int k = 0; k < nlParentRuleDetails.getLength(); k++) {

              Node parentCustomDetails = nlParentRuleDetails.item(k);

              System.

              out.println(":nlParentRuleDetails.item(k):::::::::::::::"+nlParentRuleDetails.item(k));

               

              if (parentCustomDetails.getNodeName().equals("parentcustomdetails")) {

              NamedNodeMap attributes = parentCustomDetails.getAttributes();

              System.

              out.println(":::::::::::::::::::::::"+parentCustomDetails.getNodeName());

               

              for (int l = 0; l < attributes.getLength(); l++){

              Node parentcustomcommand = attributes.item(l);

              System.

              out.println("::::::::attributes.item(l):::::::::::::::"+attributes.item(l));

              lParentExportForm.setParentcustomchildtype(parentcustomcommand.getAttribute(

              "CHILD_TYPE"));

              lParentExportForm.setParentfiltercmdcode(lParentImportDAO.findCommandCode(lParentExportDel egate.reverseCommandOperator(parentcustomcommand.getAttribute(

              "CMD_NAME")),pConnection));

               

              if(lParentExportForm.getParentfiltercmdcode()== null || lParentExportForm.getParentfiltercmdcode()==""){

              lParentExportForm.setParentfiltercmdcode(lParentImportDAO.findCommandCode(parentcustomcomm and.getAttribute(

              "CMD_NAME"),pConnection));

              }

              lParentExportForm.setParentfiltercmdname(parentcustomcommand.getAttribute(

              "CMD_NAME"));

              lParentExportForm.setParentfiltercmdoption(parentcustomcommand.getAttribute(

              "CMD_OPTION"));

              lParentExportForm.setParentfiltercustomcolumnindex(parentcustomcommand.getAttribute(

              "COLUMN_INDEX"));

              lParentExportForm.setParentfilterconditionoptr(parentcustomcommand.getAttribute(

              "CONDN_OPER"));

              lParentExportForm.setParentfilterconditionval(parentcustomcommand.getAttribute(

              "CONDN_VALUE"));

              lParentExportForm.setParentfiltertypeflag(parentcustomcommand.getAttribute(

              "RULE_TYPE_FLAG"));

              lParentImportDAO.insertParentCustomDetails(lParentExportForm,parentcode, pConnection);

              }

              • 4. Re: how to typecast string to xml Nodelist in java
                sathiyabala Level 1

                sorry again

                 

                the thing u gave is right but, if i get the nodename and nodevalue .. i cant set that value.

                 

                i need to get set the attribute and then oly i can save it in db

                 

                can u say me that pl

                 

                 

                 

                 

                new File(pFilePath);

                 

                for (int i = 0; i < children.getLength(); i++) {

                 

                if (parentRule.getNodeName().equals("Parentrule")) {

                 

                out.println(":nlParentRule::::::::INSIDE PARENTRULE::::::::::::::");

                 

                for (int j = 0; j < nlParentRule.getLength(); j++) {

                 

                if (parentRuleDetails.getNodeName().equals("parentruledetails")) {

                 

                out.println(":nlParentRule::::::::INSIDE PARENTRULEDETAILS::::::::::::::");

                 

                for (int k = 0; k < nlParentRuleDetails.getLength(); k++) {

                 

                out.println(":nlParentRuleDetails.item(k):::::::::::::::"+nlParentRuleDetails.item(k));

                 

                if (parentCustomDetails.getNodeName().equals("parentcustomdetails")) {

                 

                out.println(":::::::::::::::::::::::"+parentCustomDetails.getNodeName());

                 

                for (int l = 0; l < attributes.getLength(); l++){

                 

                out.println("::::::::attributes.item(l):::::::::::::::"+attributes.item(l));

                 

                "CHILD_TYPE"));

                 

                "CMD_NAME")),pConnection));

                 

                if(lParentExportForm.getParentfiltercmdcode()== null || lParentExportForm.getParentfiltercmdcode()==""){

                 

                "CMD_NAME"),pConnection));

                 

                "CMD_NAME"));

                 

                "CMD_OPTION"));

                 

                "COLUMN_INDEX"));

                 

                "CONDN_OPER"));

                 

                "CONDN_VALUE"));

                 

                "RULE_TYPE_FLAG"));

                lParentImportDAO.insertParentCustomDetails(lParentExportForm,parentcode, pConnection);

                }

                lParentExportForm.setParentfiltertypeflag(parentcustomcommand.getAttribute(

                lParentExportForm.setParentfilterconditionval(parentcustomcommand.getAttribute(

                lParentExportForm.setParentfilterconditionoptr(parentcustomcommand.getAttribute(

                lParentExportForm.setParentfiltercustomcolumnindex(parentcustomcommand.getAttrib ute(

                lParentExportForm.setParentfiltercmdoption(parentcustomcommand.getAttribute(

                }

                lParentExportForm.setParentfiltercmdname(parentcustomcommand.getAttribute(

                lParentExportForm.setParentfiltercmdcode(lParentImportDAO.findCommandCode(parent customcommand.getAttribute(

                 

                lParentExportForm.setParentfiltercmdcode(lParentImportDAO.findCommandCode(lParen tExportDelegate.reverseCommandOperator(parentcustomcommand.getAttribute(

                lParentExportForm.setParentcustomchildtype(parentcustomcommand.getAttribute(

                Node parentcustomcommand = attributes.item(l);

                System.

                 

                NamedNodeMap attributes = parentCustomDetails.getAttributes();

                System.

                 

                Node parentCustomDetails = nlParentRuleDetails.item(k);

                System.

                NodeList nlParentRuleDetails = parentRuleDetails.getChildNodes();

                 

                System.

                Node parentRuleDetails = nlParentRule.item(j);

                 

                 

                NodeList nlParentRule = parentRule.getChildNodes();

                System.

                Node parentRule = children.item(i);

                 

                DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();

                DocumentBuilder db = dbf.newDocumentBuilder();

                Document doc = db.parse(file);

                NodeList children = doc.getDocumentElement().getChildNodes();

                 

                File file =

                • 5. Re: how to typecast string to xml Nodelist in java
                  RK... Level 3

                  if i get the nodename and nodevalue .. i cant set that value.

                  i need to get set the attribute and then oly i can save it in db

                   

                  What does this mean? I didn't get you.