Skip to content

Commit 36b84fd

Browse files
committed
fix(xml): clean xml out of standard
1 parent 75c8c82 commit 36b84fd

File tree

4 files changed

+16
-25
lines changed

4 files changed

+16
-25
lines changed
-16 Bytes
Binary file not shown.

deploy/src/Morpho Decompose XML.py

+6-12
Original file line numberDiff line numberDiff line change
@@ -28,32 +28,26 @@
2828
except: pass
2929

3030
import System
31-
import xml.etree.ElementTree as ET
3231
from Grasshopper.Kernel.Data import GH_Path
3332
from Grasshopper import DataTree
3433
import re
34+
import clr
35+
clr.AddReference("System.Linq")
36+
clr.AddReference("System.XML.Linq")
37+
from System.Xml.Linq import XDocument
3538

3639
ghenv.Component.Message = "1.1.0"
3740

38-
39-
40-
def get_clean_xml(text):
41-
42-
characters = '[^\s()_<>/,\.A-Za-z0-9=""]+'
43-
text = re.sub(characters, '', text)
44-
return text
45-
46-
4741
def main():
4842

4943
if _XML and _keyword:
5044

51-
roots = [ET.fromstring(get_clean_xml(el)) for el in _XML]
45+
xdocs = [XDocument.Parse(el) for el in _XML]
5246
results = DataTree[System.Object]()
5347

5448
for i, key in enumerate(_keyword):
5549
path = GH_Path(0, i)
56-
results.AddRange( [root.find(key).text for root in roots if root.find(key) != None] ,path )
50+
results.AddRange( [element.Value for xdoc in xdocs for element in xdoc.Descendants(key) ] ,path )
5751

5852
return results
5953
else:

project/Morpho/Morpho25/IO/Library.cs

+4-7
Original file line numberDiff line numberDiff line change
@@ -75,17 +75,14 @@ public Library(string file,
7575

7676
private string GetCompatibleText(string file)
7777
{
78-
string characters = @"[^\s()_<>/,\.A-Za-z0-9=""]+";
79-
Encoding encoding = Encoding.UTF8;
78+
string characters = @"[^\s()_<>/,\.A-Za-z0-9=""\P{IsBasicLatin}]+";
8079

8180
if (!System.IO.File.Exists(file))
8281
throw new Exception($"{file} not found.");
83-
string text = System.IO.File.ReadAllText(file, encoding);
82+
string text = System.IO.File.ReadAllText(file);
83+
string res = Regex.Replace(text, characters, "");
8484

85-
text = System.Net.WebUtility.HtmlDecode(text);
86-
Regex.Replace(characters, "", text);
87-
88-
return text.Replace("&", "");
85+
return res;
8986
}
9087

9188
private void SetLibrary(string file, string type, string keyword)

project/Morpho/MorphoReader/Read.cs

+6-6
Original file line numberDiff line numberDiff line change
@@ -27,14 +27,14 @@ public Read(string path)
2727

2828
private string ReadEdxFile(string path)
2929
{
30-
string characters = @"[^\s()_<>/,\.A-Za-z0-9=""]+";
31-
Encoding isoLatin1 = Encoding.GetEncoding(28591); ;
32-
string text = System.IO.File.ReadAllText(path, isoLatin1);
30+
string characters = @"[^\s()_<>/,\.A-Za-z0-9=""\P{IsBasicLatin}\p{IsLatin-1Supplement}]+";
3331

34-
Regex.Replace(characters, "", text);
32+
if (!System.IO.File.Exists(path))
33+
throw new Exception($"{path} not found.");
34+
string text = System.IO.File.ReadAllText(path);
35+
string res = Regex.Replace(text, characters, "");
3536

36-
return text.Replace("&", "")
37-
.Replace("<Remark for this Source Type>", "");
37+
return res.Replace("<Remark for this Source Type>", "");
3838
}
3939

4040
private static string GetValueFromXml(XmlNodeList nodeList,

0 commit comments

Comments
 (0)