Read XML, CPython 3

Hi all,
I would like to ask about the possibility of reading xml files in the new versions (2023) of Civil 3D and Revit, where CPython 3 is available. Does anyone have any experience they are willing to share?
Regards
Lukas

to reading pure XML files, we can use ElementTree XML API (compatible both engine)

import sys
import clr
import System
from System.IO import Path
import re
pyEngineName = sys.implementation.name 
if pyEngineName == "ironpython":
    pf_path = System.Environment.GetFolderPath(System.Environment.SpecialFolder.ProgramFilesX86)
    sys.path.append(pf_path + '\\IronPython 2.7\\Lib')
	
import xml.etree.ElementTree as ET
import xml.dom.minidom
import io
out = []
path_xml = IN[0]
try:
	with open(path_xml, 'r') as o:
		txt = o.read()
except:
	with io.open(path_xml, 'r', encoding='utf-8') as o:
		txt = o.read()
		
root = xml.etree.ElementTree.fromstring(txt)    
xmlBefore = ET.tostring(root)
mySearchTags = root.findall(".//Eclairage")
for myTag in mySearchTags:
    childs = myTag.getchildren()
    for node in childs:
       out.append([myTag.tag, node.tag])

OUT =  out
4 Likes

Another example here:

2 Likes

@c.poupin Hi, I try using your code but it returns me an empty list. Do you know why?

Hi @Luffy11 ,
probably you haven’t element node named “Eclairage” in your XML file, you need to adapt the code

the document is here

@c.poupin Thank you for replying me but I don’t know code. Is there any node in Dynamo with similar function?

Before there was the LunchBox package, but the development was stopped, now I don’t know

@c.poupin yeah, I’ve installed LunchBox package but can’t find the nodes relating to XML. Let me find another way. Thank you for your reply.