0

I have a list of strings which is in xml format, but not from an xml file, so I can't use an xml parser to read in the file and parse the elements, as below:

<Pattern id="001" name="Tesla" type="car">
    <trigger>electric</trigger>
    <confidence>0.9</confidence_booster>
    <condition>
      {car ^12}
    </condition>
</Pattern>

Is there any convenient way to parse this str as a dictionary:

pattern = {"id":"001", 
          "name":"Tesla",
          "type":"car", 
          "trigger": "electric", 
          "confidence":0.9, 
          "condition": "{car ^12}"
          }

It's also fine if it can be parsed into a json object.

marlon
  • 6,029
  • 8
  • 42
  • 76
  • Why can't you use an xml parser? They don't require it to be in a file, they'll parse any string. – Barmar Jun 18 '22 at 15:30
  • I don't know about that. I will search for how to use xml parser to parse a string. Could you also write an answer? – marlon Jun 18 '22 at 15:32
  • What's to say in the answer? This is the basic way to use Beautiful Soup. AFAIK it doesn't even have a way to read from a file, you have to read from the file yourself and then pass the string. What made you think you need a file? – Barmar Jun 18 '22 at 15:34

0 Answers0