Posts

Showing posts with the label xml-parsing

Parse empty tag with ElementTree

Image
Clash Royale CLAN TAG #URR8PPP Parse empty tag with ElementTree Trying to parse XML using ElementTree. I cannot figure out how to treat empty tags like <tag/> . If the tag is not present at all, .find() returns None and everything is fine. However with <tag> , .find() returns something and the consequent attempt to call text fails with error: <tag/> .find() None <tag> .find() text TypeError: must be str, not NoneType Failing example below. It will fail to parse the line <tl><mpa/></tl> <tl><mpa/></tl> from xml.etree import ElementTree def getStuff(xml_message): message_tree = ElementTree.fromstring(xml_message) ns = {'a': 'http://www.example.org/a', 'b': 'http://www.example.org/b'} tls = message_tree.findall('.//b:tl', namespaces = ns) result, i = (0,)*2 for tl in tls: i += 1 print("Item: " + str(i)) mpa = t...

Need help parsing XML Files

Image
Clash Royale CLAN TAG #URR8PPP Need help parsing XML Files Okay So I have 3 problems with my application atm: Directory.GetFiles So here is my code for problem #1 & #2: public void patchxml() { string xmldir = AppDomain.CurrentDomain.BaseDirectory + @"\tmp"; string xmlfile = Directory.GetFiles(xmldir, "*.xml"); XDocument xdoc = XDocument.Load(xmlfile[0]); //var element = xdoc.Elements("RequiredSystemVersion").Single(); not working //element.Value = "0"; not working //xdoc.Save(); not working } Here Is a sample of the xml file: <?xml version="1.0" encoding="utf-8"?> <ContentMeta> <RequiredSystemVersion>335675392</RequiredSystemVersion> <PatchId>0x01009bf0072d4800</PatchId> </ContentMeta> What I need to do is, that I need to change the required system version to...

A c# program that searches through multiple XML files and text to certain elements inside

Image
Clash Royale CLAN TAG #URR8PPP A c# program that searches through multiple XML files and text to certain elements inside I need to create a c# program that searches through multiple XML files and adds a post fix entered by the user to certain elements inside those XML files, those elements all have the string "_name" inside them, which is a blessing considering now I know what extra condition I can use to make sure I'm altering the correct elements, My basic understanding of how the process is gonna go is like this: 1- I let user browse through his computer and open the folder in which the XML files exist. 2- I let the program add "main" to the chosen path and so open the main.xml file which contains all the files I'll be needing to alter. (done) 3- create a loop that will open each file inside the main.xml file and store all its' contents inside a list of strings. 4- now compare each element inside the list with the content of each folder, and if a matc...

Parse XML document with DOM parser, with multiple elements for each tag

Image
Clash Royale CLAN TAG #URR8PPP Parse XML document with DOM parser, with multiple elements for each tag I need to parse a XML document wich has same tag names. I'm giving you a sample of this code to see what I want to do.. <SystemData> <SystemName>xmlexample</SystemName> <Schools> <School> <SchoolName>SCHOOL1</SchoolName> <Classes> <Class> <ClassName>ACLASS</ClassName> </Class> </Classes> <Classes> <Class> <ClassName>BCLASS</ClassName> </Class> </Classes> </School> <School> <SchoolName>SCHOOL2</SchoolName> <Classes> <Class> <ClassName>CCLASS</ClassName> ...