Untitled
unknown
plain_text
a year ago
2.8 kB
16
Indexable
import xml.etree.ElementTree as ET
def convert_cap_to_aea(cap_xml):
cap_tree = ET.ElementTree(ET.fromstring(cap_xml))
cap_root = cap_tree.getroot()
aea_root = ET.Element("AEA")
# Mapping elements from CAP to AEA
aea_identifier = ET.SubElement(aea_root, "identifier")
aea_identifier.text = cap_root.find("identifier").text
aea_issuer = ET.SubElement(aea_root, "issuer")
aea_issuer.text = cap_root.find("sender").text
aea_issuedTime = ET.SubElement(aea_root, "issuedTime")
aea_issuedTime.text = cap_root.find("sent").text
info = cap_root.find("info")
aea_alertCategory = ET.SubElement(aea_root, "alertCategory")
aea_alertCategory.text = info.find("category").text
aea_alertEvent = ET.SubElement(aea_root, "alertEvent")
aea_alertEvent.text = info.find("event").text
aea_alertUrgency = ET.SubElement(aea_root, "alertUrgency")
aea_alertUrgency.text = info.find("urgency").text
aea_alertSeverity = ET.SubElement(aea_root, "alertSeverity")
aea_alertSeverity.text = info.find("severity").text
aea_alertCertainty = ET.SubElement(aea_root, "alertCertainty")
aea_alertCertainty.text = info.find("certainty").text
aea_alertText = ET.SubElement(aea_root, "alertText")
aea_alertText.text = info.find("description").text
aea_alertInstruction = ET.SubElement(aea_root, "alertInstruction")
aea_alertInstruction.text = info.find("instruction").text
area = info.find("area")
aea_targetArea = ET.SubElement(aea_root, "targetArea")
aea_areaDescription = ET.SubElement(aea_targetArea, "areaDescription")
aea_areaDescription.text = area.find("areaDesc").text
polygon = area.find("polygon")
if polygon is not None:
aea_polygon = ET.SubElement(aea_targetArea, "polygon")
aea_polygon.text = polygon.text
circle = area.find("circle")
if circle is not None:
aea_circle = ET.SubElement(aea_targetArea, "circle")
aea_circle.text = circle.text
# Return the AEA XML as a string
aea_xml = ET.tostring(aea_root, encoding='unicode')
return aea_xml
# Exemplo de uso
cap_example = """<alert>
<identifier>EX1234567890</identifier>
<sender>sender@example.com</sender>
<sent>2024-07-31T12:34:56-00:00</sent>
<status>Actual</status>
<msgType>Alert</msgType>
<scope>Public</scope>
<info>
<category>Met</category>
<event>Severe Thunderstorm Warning</event>
<urgency>Immediate</urgency>
<severity>Severe</severity>
<certainty>Observed</certainty>
<description>Severe thunderstorms are occurring.</description>
<instruction>Take cover now.</instruction>
<area>
<areaDesc>City of Example</areaDesc>
<polygon>-90.0,30.0 -91.0,30.0 -91.0,31.0 -90.0,31.0</polygon>
</area>
</info>
</alert>"""
aea_xml = convert_cap_to_aea(cap_example)
print(aea_xml)Editor is loading...
Leave a Comment