• header.categories
    • header.recent
    • header.tags
    • header.popular
    • register
    • login

    possible to read xml file in custom widget?

    scheduled pinned locked moved General Discussion
    16 posts 2 posters 3.3k views 1 watching
    loading-more-posts
    • oldest-to-newest
    • newest-to-oldest
    • most-votes
    reply
    • reply-as-topic
    guest-login-reply
    deleted-message
    • A offline
      Anduril
      global:last-edited-by,

      Thanks a lot four your code, I will try to refine this and get it working. I will have a local xml file in future, but I think I will find a good tutorial for this in the web.

      one-reply-to-this-post last-reply-time reply quote 0
      • A offline
        Anduril
        global:last-edited-by,

        hi,

        is it also possible to read a local xml file? And how can I access the different information in the file, are they transfered to the xmlData as options? So would this file:

        <?xml version="1.0" encoding="UTF-8"?>
        <name id="1">
          <first>John</first>
          <last>Doe</last>
        </name>
        

        be read as for example xmlData.name.first

        jkandasaJ one-reply-to-this-post last-reply-time reply quote 0
        • jkandasaJ offline
          jkandasa @Anduril
          global:last-edited-by,

          @anduril It is possible to read XML file. But as we need to write some implementation to achieve xmlData.name.first. Kindly create a ticket for this.

          one-reply-to-this-post last-reply-time reply quote 0
          • A offline
            Anduril
            global:last-edited-by,

            well the way to access the data is (in my case) not important, I could also use a static parser. I want to open several xml files with all the same structure and extract the data to mycontroller variables. Those could be read and processed in a second step by another script.

            one-reply-to-this-post last-reply-time reply quote 0
            • A offline
              Anduril
              global:last-edited-by,

              well I did also tried your example code and it seems mcApi.http().get does not work correctly...

              TypeError: mcApi.http is not a function in  at line number 4
              
              jkandasaJ one-reply-to-this-post last-reply-time reply quote 0
              • jkandasaJ offline
                jkandasa @Anduril
                global:last-edited-by,

                @anduril Can you post your complete code?

                I tested example code once again with SNAPSHOT build, seems everything ok.

                {
                  "myImports": {},
                  "response": {
                    "uri": "http://www.plus2net.com/php_tutorial/file-xml-demo.xml",
                    "entity": "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<student>\n<details>\n\t\t\t<id>1</id>\n\t\t\t<name>John Deo</name>\n\t\t\t<class>Four</class>\n</details>\n<details>\n\t\t\t<id>2</id>\n\t\t\t<name>Max Ruin</name>\n\t\t\t<class>Three</class>\n</details>\n<details>\n\t\t\t<id>3</id>\n\t\t\t<name>Arnold</name>\n\t\t\t<class>Three</class>\n</details>\n<details>\n\t\t\t<id>4</id>\n\t\t\t<name>Krish Star</name>\n\t\t\t<class>Four</class>\n</details>\n<details>\n\t\t\t<id>5</id>\n\t\t\t<name>John Mike</name>\n\t\t\t<class>Four</class>\n</details>\n<details>\n\t\t\t<id>6</id>\n\t\t\t<name>Alex John</name>\n\t\t\t<class>Four</class>\n</details>\n<details>\n\t\t\t<id>7</id>\n\t\t\t<name>My John Rob</name>\n\t\t\t<class>Fifth</class>\n</details>\n<details>\n\t\t\t<id>8</id>\n\t\t\t<name>Asruid</name>\n\t\t\t<class>Five</class>\n</details>\n<details>\n\t\t\t<id>9</id>\n\t\t\t<name>Tes Qry</name>\n\t\t\t<class>Six</class>\n</details>\n<details>\n\t\t\t<id>10</id>\n\t\t\t<name>Big John</name>\n\t\t\t<class>Four</class>\n</details>\n<details>\n\t\t\t<id>12</id>\n\t\t\t<name>Recky</name>\n\t\t\t<class>Six</class>\n</details>\n<details>\n\t\t\t<id>13</id>\n\t\t\t<name>Kty</name>\n\t\t\t<class>Seven</class>\n</details>\n<details>\n\t\t\t<id>14</id>\n\t\t\t<name>Bigy</name>\n\t\t\t<class>Seven</class>\n</details>\n<details>\n\t\t\t<id>15</id>\n\t\t\t<name>Tade Row</name>\n\t\t\t<class>Four</class>\n</details>\n<details>\n\t\t\t<id>16</id>\n\t\t\t<name>Gimmy</name>\n\t\t\t<class>Four</class>\n</details>\n<details>\n\t\t\t<id>17</id>\n\t\t\t<name>Tumyu</name>\n\t\t\t<class>Six</class>\n</details>\n<details>\n\t\t\t<id>18</id>\n\t\t\t<name>Honny</name>\n\t\t\t<class>Five</class>\n</details>\n<details>\n\t\t\t<id>19</id>\n\t\t\t<name>Tinny</name>\n\t\t\t<class>Nine</class>\n</details>\n<details>\n\t\t\t<id>20</id>\n\t\t\t<name>Jackly</name>\n\t\t\t<class>Nine</class>\n</details>\n<details>\n\t\t\t<id>21</id>\n\t\t\t<name>Babby John</name>\n\t\t\t<class>Four</class>\n</details>\n<details>\n\t\t\t<id>22</id>\n\t\t\t<name>Reggid</name>\n\t\t\t<class>Seven</class>\n</details>\n<details>\n\t\t\t<id>23</id>\n\t\t\t<name>Herod</name>\n\t\t\t<class>Eight</class>\n</details>\n<details>\n\t\t\t<id>24</id>\n\t\t\t<name>Tiddy Now</name>\n\t\t\t<class>Seven</class>\n</details>\n<details>\n\t\t\t<id>25</id>\n\t\t\t<name>Giff Tow</name>\n\t\t\t<class>Seven</class>\n</details>\n<details>\n\t\t\t<id>26</id>\n\t\t\t<name>Crelea</name>\n\t\t\t<class>Seven</class>\n</details>\n<details>\n\t\t\t<id>27</id>\n\t\t\t<name>Big Nose</name>\n\t\t\t<class>Three</class>\n</details>\n<details>\n\t\t\t<id>28</id>\n\t\t\t<name>Rojj Base</name>\n\t\t\t<class>Seven</class>\n</details>\n<details>\n\t\t\t<id>29</id>\n\t\t\t<name>Tess Played</name>\n\t\t\t<class>Seven</class>\n</details>\n<details>\n\t\t\t<id>30</id>\n\t\t\t<name>Reppy Red</name>\n\t\t\t<class>Six</class>\n</details>\n<details>\n\t\t\t<id>31</id>\n\t\t\t<name>Marry Toeey</name>\n\t\t\t<class>Four</class>\n</details>\n<details>\n\t\t\t<id>32</id>\n\t\t\t<name>Binn Rott</name>\n\t\t\t<class>Seven</class>\n</details>\n<details>\n\t\t\t<id>33</id>\n\t\t\t<name>Kenn Rein</name>\n\t\t\t<class>Six</class>\n</details>\n<details>\n\t\t\t<id>34</id>\n\t\t\t<name>Gain Toe</name>\n\t\t\t<class>Seven</class>\n</details>\n<details>\n\t\t\t<id>35</id>\n\t\t\t<name>Rows Noump</name>\n\t\t\t<class>Six</class>\n</details>\n</student>",
                    "responseCode": 200,
                    "headers": [
                      {
                        "name": "Date",
                        "buffer": {
                          "full": false,
                          "empty": false
                        },
                        "valuePos": 5,
                        "elements": [
                          {
                            "name": "Mon",
                            "parameters": [],
                            "parameterCount": 0
                          },
                          {
                            "name": "30 Apr 2018 16:24:19 GMT",
                            "parameters": [],
                            "parameterCount": 0
                          }
                        ],
                        "value": "Mon, 30 Apr 2018 16:24:19 GMT"
                      },
                      {
                        "name": "Server",
                        "buffer": {
                          "full": false,
                          "empty": false
                        },
                        "valuePos": 7,
                        "elements": [
                          {
                            "name": "Apache",
                            "parameters": [],
                            "parameterCount": 0
                          }
                        ],
                        "value": "Apache"
                      },
                      {
                        "name": "Last-Modified",
                        "buffer": {
                          "full": false,
                          "empty": false
                        },
                        "valuePos": 14,
                        "elements": [
                          {
                            "name": "Sun",
                            "parameters": [],
                            "parameterCount": 0
                          },
                          {
                            "name": "07 Aug 2016 04:44:57 GMT",
                            "parameters": [],
                            "parameterCount": 0
                          }
                        ],
                        "value": "Sun, 07 Aug 2016 04:44:57 GMT"
                      },
                      {
                        "name": "ETag",
                        "buffer": {
                          "full": false,
                          "empty": false
                        },
                        "valuePos": 5,
                        "elements": [
                          {
                            "name": "\"11e3021-b53-53973f62fcef3-gzip\"",
                            "parameters": [],
                            "parameterCount": 0
                          }
                        ],
                        "value": "\"11e3021-b53-53973f62fcef3-gzip\""
                      },
                      {
                        "name": "Accept-Ranges",
                        "buffer": {
                          "full": false,
                          "empty": false
                        },
                        "valuePos": 14,
                        "elements": [
                          {
                            "name": "bytes",
                            "parameters": [],
                            "parameterCount": 0
                          }
                        ],
                        "value": "bytes"
                      },
                      {
                        "name": "Cache-Control",
                        "buffer": {
                          "full": false,
                          "empty": false
                        },
                        "valuePos": 14,
                        "elements": [
                          {
                            "name": "max-age",
                            "value": "172800",
                            "parameters": [],
                            "parameterCount": 0
                          }
                        ],
                        "value": "max-age=172800"
                      },
                      {
                        "name": "Expires",
                        "buffer": {
                          "full": false,
                          "empty": false
                        },
                        "valuePos": 8,
                        "elements": [
                          {
                            "name": "Wed",
                            "parameters": [],
                            "parameterCount": 0
                          },
                          {
                            "name": "02 May 2018 16:24:19 GMT",
                            "parameters": [],
                            "parameterCount": 0
                          }
                        ],
                        "value": "Wed, 02 May 2018 16:24:19 GMT"
                      },
                      {
                        "name": "Vary",
                        "buffer": {
                          "full": false,
                          "empty": false
                        },
                        "valuePos": 5,
                        "elements": [
                          {
                            "name": "Accept-Encoding",
                            "parameters": [],
                            "parameterCount": 0
                          },
                          {
                            "name": "User-Agent",
                            "parameters": [],
                            "parameterCount": 0
                          }
                        ],
                        "value": "Accept-Encoding,User-Agent"
                      },
                      {
                        "name": "Keep-Alive",
                        "buffer": {
                          "full": false,
                          "empty": false
                        },
                        "valuePos": 11,
                        "elements": [
                          {
                            "name": "timeout",
                            "value": "5",
                            "parameters": [],
                            "parameterCount": 0
                          }
                        ],
                        "value": "timeout=5"
                      },
                      {
                        "name": "Connection",
                        "buffer": {
                          "full": false,
                          "empty": false
                        },
                        "valuePos": 11,
                        "elements": [
                          {
                            "name": "Keep-Alive",
                            "parameters": [],
                            "parameterCount": 0
                          }
                        ],
                        "value": "Keep-Alive"
                      },
                      {
                        "name": "Content-Type",
                        "buffer": {
                          "full": false,
                          "empty": false
                        },
                        "valuePos": 13,
                        "elements": [
                          {
                            "name": "application/xml",
                            "parameters": [],
                            "parameterCount": 0
                          }
                        ],
                        "value": "application/xml"
                      }
                    ]
                  },
                  "xmlData": "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<student>\n<details>\n\t\t\t<id>1</id>\n\t\t\t<name>John Deo</name>\n\t\t\t<class>Four</class>\n</details>\n<details>\n\t\t\t<id>2</id>\n\t\t\t<name>Max Ruin</name>\n\t\t\t<class>Three</class>\n</details>\n<details>\n\t\t\t<id>3</id>\n\t\t\t<name>Arnold</name>\n\t\t\t<class>Three</class>\n</details>\n<details>\n\t\t\t<id>4</id>\n\t\t\t<name>Krish Star</name>\n\t\t\t<class>Four</class>\n</details>\n<details>\n\t\t\t<id>5</id>\n\t\t\t<name>John Mike</name>\n\t\t\t<class>Four</class>\n</details>\n<details>\n\t\t\t<id>6</id>\n\t\t\t<name>Alex John</name>\n\t\t\t<class>Four</class>\n</details>\n<details>\n\t\t\t<id>7</id>\n\t\t\t<name>My John Rob</name>\n\t\t\t<class>Fifth</class>\n</details>\n<details>\n\t\t\t<id>8</id>\n\t\t\t<name>Asruid</name>\n\t\t\t<class>Five</class>\n</details>\n<details>\n\t\t\t<id>9</id>\n\t\t\t<name>Tes Qry</name>\n\t\t\t<class>Six</class>\n</details>\n<details>\n\t\t\t<id>10</id>\n\t\t\t<name>Big John</name>\n\t\t\t<class>Four</class>\n</details>\n<details>\n\t\t\t<id>12</id>\n\t\t\t<name>Recky</name>\n\t\t\t<class>Six</class>\n</details>\n<details>\n\t\t\t<id>13</id>\n\t\t\t<name>Kty</name>\n\t\t\t<class>Seven</class>\n</details>\n<details>\n\t\t\t<id>14</id>\n\t\t\t<name>Bigy</name>\n\t\t\t<class>Seven</class>\n</details>\n<details>\n\t\t\t<id>15</id>\n\t\t\t<name>Tade Row</name>\n\t\t\t<class>Four</class>\n</details>\n<details>\n\t\t\t<id>16</id>\n\t\t\t<name>Gimmy</name>\n\t\t\t<class>Four</class>\n</details>\n<details>\n\t\t\t<id>17</id>\n\t\t\t<name>Tumyu</name>\n\t\t\t<class>Six</class>\n</details>\n<details>\n\t\t\t<id>18</id>\n\t\t\t<name>Honny</name>\n\t\t\t<class>Five</class>\n</details>\n<details>\n\t\t\t<id>19</id>\n\t\t\t<name>Tinny</name>\n\t\t\t<class>Nine</class>\n</details>\n<details>\n\t\t\t<id>20</id>\n\t\t\t<name>Jackly</name>\n\t\t\t<class>Nine</class>\n</details>\n<details>\n\t\t\t<id>21</id>\n\t\t\t<name>Babby John</name>\n\t\t\t<class>Four</class>\n</details>\n<details>\n\t\t\t<id>22</id>\n\t\t\t<name>Reggid</name>\n\t\t\t<class>Seven</class>\n</details>\n<details>\n\t\t\t<id>23</id>\n\t\t\t<name>Herod</name>\n\t\t\t<class>Eight</class>\n</details>\n<details>\n\t\t\t<id>24</id>\n\t\t\t<name>Tiddy Now</name>\n\t\t\t<class>Seven</class>\n</details>\n<details>\n\t\t\t<id>25</id>\n\t\t\t<name>Giff Tow</name>\n\t\t\t<class>Seven</class>\n</details>\n<details>\n\t\t\t<id>26</id>\n\t\t\t<name>Crelea</name>\n\t\t\t<class>Seven</class>\n</details>\n<details>\n\t\t\t<id>27</id>\n\t\t\t<name>Big Nose</name>\n\t\t\t<class>Three</class>\n</details>\n<details>\n\t\t\t<id>28</id>\n\t\t\t<name>Rojj Base</name>\n\t\t\t<class>Seven</class>\n</details>\n<details>\n\t\t\t<id>29</id>\n\t\t\t<name>Tess Played</name>\n\t\t\t<class>Seven</class>\n</details>\n<details>\n\t\t\t<id>30</id>\n\t\t\t<name>Reppy Red</name>\n\t\t\t<class>Six</class>\n</details>\n<details>\n\t\t\t<id>31</id>\n\t\t\t<name>Marry Toeey</name>\n\t\t\t<class>Four</class>\n</details>\n<details>\n\t\t\t<id>32</id>\n\t\t\t<name>Binn Rott</name>\n\t\t\t<class>Seven</class>\n</details>\n<details>\n\t\t\t<id>33</id>\n\t\t\t<name>Kenn Rein</name>\n\t\t\t<class>Six</class>\n</details>\n<details>\n\t\t\t<id>34</id>\n\t\t\t<name>Gain Toe</name>\n\t\t\t<class>Seven</class>\n</details>\n<details>\n\t\t\t<id>35</id>\n\t\t\t<name>Rows Noump</name>\n\t\t\t<class>Six</class>\n</details>\n</student>"
                }
                
                one-reply-to-this-post last-reply-time reply quote 0
                • A offline
                  Anduril
                  global:last-edited-by, Anduril

                  thanks @jkandasa, it was probably a problem solved with the most recent snapshot. I updated and it works now with the online xml.
                  Using a local xml still yields an error, I think there is an error in my call...

                  var myImports = new JavaImporter(java.io, java.lang, java.util, java.text);
                  
                  with(myImports) {
                   var response = mcApi.http().get("c:/file-xml-demo.xml");
                   if(response.responseCode == 200){
                      var xmlData = response.entity;
                   }
                  }
                  

                  using [...]get("c:\file-[...] does not work either, so it's not a problem with slash and backslash.
                  The html file only contains this:

                  ${xmlData}
                  

                  Using the run nowoption I get this error:

                  TypeError: Cannot get property "responseCode" of null in  at line number 5
                  
                  jkandasaJ one-reply-to-this-post last-reply-time reply quote 0
                  • jkandasaJ offline
                    jkandasa @Anduril
                    global:last-edited-by,

                    @anduril mcApi.http() used to access from network/internet. We cannot use it to access local disk files.

                    To access local disk files, you can use the following program,

                    var myImports = new JavaImporter(java.io, java.lang, java.util, java.text, org.apache.commons.io.FileUtils);
                    
                    with(myImports) {
                     var xmlData = FileUtils.readFileToString(FileUtils.getFile("/tmp/file-xml-demo.xml"));
                    }
                    
                    one-reply-to-this-post last-reply-time reply quote 0
                    • A offline
                      Anduril
                      global:last-edited-by,

                      Thanks a lot, it works that way. Now my next problem is how to find specific information in this data. I could try to parse line by line, as I have a fixed structure. But maybe it would be easier to create a generic xml parsing mechanism. This would also allow for easier export to xml (my next step when reading works flawless).

                      jkandasaJ topic:replies-to-this-post, 2 last-reply-time reply quote 0
                      • jkandasaJ offline
                        jkandasa @Anduril
                        global:last-edited-by,

                        https://github.com/mycontroller-org/mycontroller/issues/449

                        one-reply-to-this-post last-reply-time reply quote 0
                        • jkandasaJ offline
                          jkandasa @Anduril
                          global:last-edited-by, jkandasa

                          @Anduril Feature available in SNAPSHOT version.

                          you can read xml content by xpath. To know more about xpath do google or have a look on https://www.w3schools.com/xml/xpath_intro.asp

                          Function/Method: mcApi.xml().read(uri, xpath) - Returns String

                          • uri - can be local disk path or http url
                          • xpath - should be a valid xpath
                          var myImports = new JavaImporter(java.io, java.lang, java.util, java.text);
                          
                          with(myImports) {
                           var myData = mcApi.xml().read("http://www.plus2net.com/php_tutorial/file-xml-demo.xml", "/student/details/id[text()='3']/../name");
                          }
                          

                          Some of xpath example:

                          XML content,

                          <?xml version="1.0" encoding="UTF-8"?>
                          <student>
                            <details>
                              <id>1</id>
                              <name>John Deo</name>
                              <class>Four</class>
                            </details>
                            <details>
                              <id>2</id>
                              <name>Max Ruin</name>
                              <class>Three</class>
                            </details>
                            <details>
                              <id>3</id>
                              <name>Arnold</name>
                              <class>Three</class>
                            </details>
                            <details>
                              <id>4</id>
                              <name>Krish Star</name>
                              <class>Four</class>
                            </details>
                            <details>
                              <id>5</id>
                              <name>John Mike</name>
                              <class>Four</class>
                            </details>
                          </student>
                          
                          • Read student:
                            • /student
                          • Read name where id is 4:
                            • /student/details/id[text()='4']/../name
                            • //id[text()='4']/../name
                          • Read class of 2nd element:
                            • /student/details[2]/class
                            • //details[2]/class
                          • Read id where the name is John Mike:
                            • /student/details/name[text()='John Mike']/../id
                            • //name[text()='John Mike']/../id
                          one-reply-to-this-post last-reply-time reply quote 0
                          • A offline
                            Anduril
                            global:last-edited-by,

                            wow that's great... thanks a lot.

                            one-reply-to-this-post last-reply-time reply quote 0
                            • A offline
                              Anduril
                              global:last-edited-by,

                              This feature is great (at least for my usage). Is it also possible to update the values in the xml file? I don't mean adding new nodes or other fancy stuff, only updating for example /student/details/class from Threeto Four.
                              As far as I read the xpath is just a pointer in the tree of an xml, so there is no option to manipulate items. Maybe there is another function available to do this.

                              jkandasaJ one-reply-to-this-post last-reply-time reply quote 0
                              • jkandasaJ offline
                                jkandasa @Anduril
                                global:last-edited-by,

                                @anduril I have removed the old method readXmlByXpath. Introduced XmlApi class to address xml stuff.

                                • mcApi.xml().read(uri, xpath) - Returns String
                                  • uri - can be local disk path or http url
                                  • xpath - should be a valid xpath
                                • mcApi.xml().readAsList(uri, xpath) - Returns List<Map<String, String>>
                                  • uri - can be local disk path or http url
                                  • xpath - should be a valid xpath
                                • mcApi.xml().update(uri, xpath, value)
                                  • uri - should be local disk path
                                  • xpath - should be a valid xpath
                                  • value - String value will be replaced on xpath

                                Is it also possible to update the values in the xml file? I don't mean adding new nodes or other fancy stuff, only updating for example /student/details/class from Three to Four.

                                Yes, now it is possible with,

                                mcApi.xml().update("/tmp/file-xml-demo.xml", "/student/details[1]/class", "Four");
                                

                                As far as I read the xpath is just a pointer in the tree of an xml, so there is no option to manipulate items. Maybe there is another function available to do this.

                                You can call readAsList,

                                var list = mcApi.xml().readAsList("/tmp/file-xml-demo.xml", "//id[text()='4']/..");
                                // get first result
                                var item = list.get(0);
                                
                                // get name, id, class
                                item.get("name");
                                item.get("id");
                                item.get("class");
                                
                                one-reply-to-this-post last-reply-time reply quote 0
                                • first-post
                                  last-post

                                0

                                online

                                644

                                users

                                532

                                topics

                                3.4k

                                posts
                                Copyright © 2015-2025 MyController.org | Contributors | Localization