0

What are my options if I want to create a sample XML file in python?

The xml I want looks like is below also i want to take some input from user through cmdline arguments for example Nodename input came from user in ID only Machine field came from user input .

<?xml version="1.0" ?>
<Windows 
MachineClass="Windows"
xmlns="https://graphit.co/schemas/v2/MARSSchema"
ID="abc.com:brd-del:Machine:LDN1HAS1"
NodeName="LDN1HAS1"
NodeType="Machine"
CustomerID="abc.com"
CustomerName="abc.com"
OSName="Windows2016"
OSMajorVersion="10"
OSMinorVersion="0">
<SourceCiId><Content Value="LDN1HAS1"/></SourceCiId>
<FQDN><Content Value="LDN1HAS1.abc.com"/></FQDN>
<IPAddress><Content Value="10.101.248.128"/></IPAddress>
<Location><Content Value="London"/></Location>
<EnvironmentClassifier><Content Value="Production"/></EnvironmentClassifier>
</Windows>
  • 1
    Possible duplicate of [Creating a simple XML file using python](https://stackoverflow.com/questions/3605680/creating-a-simple-xml-file-using-python) – Grismar Sep 12 '19 at 04:41

1 Answers1

1
import xml.etree.cElementTree as ET

root = ET.Element("root")
doc = ET.SubElement(root, "doc")

ET.SubElement(doc, "field1", name="blah").text = "some value1"
ET.SubElement(doc, "field2", name="asdfasd").text = "some vlaue2"

tree = ET.ElementTree(root)
tree.write("filename.xml")

for example.

see: Creating a simple XML file using python for more

Ruben Helsloot
  • 12,582
  • 6
  • 26
  • 49
ab123
  • 357
  • 4
  • 17