mycontroller-org / mycontroller-v1-legacy

The Open Source Controller
http://www.MyController.org
Apache License 2.0
148 stars 90 forks source link

extract structured information from xml #449

Closed AnduriI closed 6 years ago

AnduriI commented 6 years ago

Hey,

I imported a xml file, but the information in it can not be read in any structured way. Maybe we can find a way to select a single information from this xml via it's hierarchy. (discussed in https://forum.mycontroller.org/topic/193/possible-to-read-xml-file-in-custom-widget/5)

Best regards, Anduril

jkandasa commented 6 years ago

@AnduriI 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

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>
jkandasa commented 6 years ago

@AnduriI new methods are introduced,

AnduriI commented 6 years ago

Again, a big thanks. The thing you implemented works great. Can I also use dynamic names for the uri or xpath statement? I tried to use a variable to define the file path, but that didn't worked: mcApi.xml().update("D:/Downloads/${filename}.xml", "//ID" , 10); java.net.MalformedURLException: unknown protocol: c

jkandasa commented 6 years ago

@AnduriI I think this line should be as,

mcApi.xml().update("D:/Downloads/" + filename + ".xml", "//ID" , 10);
AnduriI commented 6 years ago

thanks a lot