6 Replies Latest reply on Sep 18, 2015 12:38 AM by Vamitul

    read xml

    Level 1

      I need to read the xml file and get the attribute value

       

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

      <SOURCE>

          <Paragraph ParaNumber="Pg_1_P_3"/>

          <Paragraph ParaNumber="Pg_1_P_4"/>

         

      </SOURCE>

       

      var outmyf=Folder.selectDialog("Choose Out folder path");

      var FilePath=new File(outmyf+"/PageNumber.xml");

      //alert (FilePath)

      var xmlFile = new File(FilePath);

      if(xmlFile.exists)

      {

          var xmlFileOpenFlag = xmlFile.open("r");

          if(xmlFileOpenFlag == true)

          {

              var xmlFileContents = xmlFile.read();

              var root = new XML(xmlFileContents); 

              var Pageno = root.xpath("/SOURCE/Paragraph[@ParaNumber]"); 

          }

      }

       

      But I need only the attribute value of the xml element the above code returning all the values.

        • 1. Re: read xml
          Ronald63 Level 4

          Hi,

           

          Try this...

          var outmyf=Folder.selectDialog("Choose Out folder path");
          var FilePath=new File(outmyf+"/PageNumber.xml");
          //alert (FilePath)
          var xmlFile = new File(FilePath);
          if(xmlFile.exists)
          {
              var xmlFileOpenFlag = xmlFile.open("r");
              if(xmlFileOpenFlag == true)
              {
                  var xmlFileContents = xmlFile.read();
                  var root = new XML(xmlFileContents);
                  var Pageno = root.xpath("/*/@ParaNumber"); 
                  len= Pageno.length();
                  while (len --){
                      var ParaNumberValue = Pageno[len];
                      alert(ParaNumberValue);
                  }
              }
          }
          

          Regards

          • 2. Re: read xml
            Marc Autret Level 4

            Note that root.xpath("/*/@ParaNumber") can be abbreviated root.*.@ParaNumber (this returns the same XMLList).

             

            Also, the para numbers can be collected into an Array this way:

            var a = root.*.@ParaNumber.toXMLString().split(/[\r\n]+/);

            alert( a );


            @+

            Marc

            • 3. Re: read xml
              Level 1

              Its working marc thanks for this.

              • 4. Re: read xml
                Loic.Aigon Adobe Community Professional

                You can even go straighter

                 

                x..@ParaNumber

                 

                The only interest in using an intermediate state would have been to only adress @ParaNumber whose parent is X or Y like

                 

                x..foo.@ParaNumber

                 

                FWIW,

                 

                Loic

                • 5. Re: read xml
                  Loic.Aigon Adobe Community Professional

                  And you don't need to force an array conversion as a XMLList is an array-like.

                   

                  var myAttrs = x..@ParaNumber;
                  var n = myAttrs.length();
                  
                  
                  while ( n-- ) {
                    $.writeln ("n:"+ myAttrs[n] );
                  }
                  

                   

                  FWIW,

                   

                  Loic

                  • 6. Re: read xml
                    Vamitul Level 4

                    One weirdness to note, both approaches will fail if the xml only contains one instance of the searched element:
                    root.xpath(/*/Para).nodeKind()->Element if only one found (length() is 0)

                    root.xpath(/*/Para).nodeKind()->XMLList if more than one found (length() is whatever)