Prabhat Jaiswal
Prabhat Jaiswal

Reputation: 23

Creating a sample XML file using python

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>

Upvotes: 0

Views: 138

Answers (1)

ab123
ab123

Reputation: 357

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

Upvotes: 1

Related Questions