Closed AnduriI closed 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");
}
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>
student
:
/student
name
where id
is 4
:
/student/details/id[text()='4']/../name
//id[text()='4']/../name
class
of 2nd element:
/student/details[2]/class
//details[2]/class
id
where the name
is John Mike
:
/student/details/name[text()='John Mike']/../id
//name[text()='John Mike']/../id
@AnduriI new methods are introduced,
mcApi.xml().read(uri, xpath)
- Returns String
uri
- can be local disk path or http urlxpath
- should be a valid xpathmcApi.xml().readAsList(uri, xpath)
- Returns List<Map<String, String>>
uri
- can be local disk path or http urlxpath
- should be a valid xpathmcApi.xml().update(uri, xpath, value)
uri
- should be local disk pathxpath
- should be a valid xpathvalue
- String value will be replaced on xpathAgain, 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
@AnduriI I think this line should be as,
mcApi.xml().update("D:/Downloads/" + filename + ".xml", "//ID" , 10);
thanks a lot
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