如何使用python按字母顺序对XML排序?

2024-05-14 01:18:09 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一些XML文件要按元素名排序。这些xml文件被视为my salesforce sandbox/org中的配置文件。我编写了一些代码,将一个xml文件附加到每个配置文件xml文件的底部。允许我一次向多个文件添加代码,而不必复制/粘贴到每个文件。这里的问题是,xml需要按元素名称的字母顺序排序,例如:(ClassAccess、fieldPermissions、layoutAssignments、recordTypeVisibilities、objectPermissions)我在下面粘贴了一个xml示例。文件的格式需要一致,不能更改,因为salesforce可能不喜欢它

<?xml version="1.0" encoding="UTF-8"?>
<Profile xmlns="http://soap.sforce.com/2006/04/metadata">
    <fieldPermissions>
        <editable>false</editable>
        <field>Branch_Queue__c.Cell_Phone_Number__c</field>
        <readable>true</readable>
    </fieldPermissions>
    <fieldPermissions>
        <editable>false</editable>
        <field>Branch_Queue__c.Branch__c</field>
        <readable>true</readable>
    </fieldPermissions>
    <fieldPermissions>
        <editable>false</editable>
        <field>Branch_Queue__c.Source__c</field>
        <readable>true</readable>
    </fieldPermissions>
    <fieldPermissions>
        <editable>false</editable>
        <field>Branch_Queue__c.Served_By__c</field>
        <readable>true</readable>
    </fieldPermissions>
    <fieldPermissions>
            <editable>false</editable>
            <field>Branch_Queue__c.Update__c</field>
            <readable>true</readable>
    </fieldPermissions>
    <recordTypeVisibilities>
        <default>false</default>
        <recordType>Knowledge__kav.RealEstate</recordType>
        <visible>true</visible>
    </recordTypeVisibilities>
    <recordTypeVisibilities>
        <default>false</default>
        <recordType>Knowledge__kav.RealEstate_Community_Connection</recordType>
        <visible>true</visible>
    </recordTypeVisibilities>
     <objectPermissions>
        <allowCreate>false</allowCreate>
        <allowDelete>false</allowDelete>
        <allowEdit>false</allowEdit>
        <allowRead>true</allowRead>
        <modifyAllRecords>false</modifyAllRecords>
        <object>Branch_Queue__c</object>
        <viewAllRecords>true</viewAllRecords>
    </objectPermissions>
    <classAccesses>
        <apexClass>BranchQueueDisplayList</apexClass>
        <enabled>true</enabled>
    </classAccesses>
    <classAccesses>
        <apexClass>BranchQueueDisplayList_Test</apexClass>
        <enabled>true</enabled>
    </classAccesses>
    <classAccesses>
        <apexClass>BranchQueueService</apexClass>
        <enabled>true</enabled>
    </classAccesses>
</Profile>

如果有帮助,下面是我构建的python脚本。如果您有任何问题,请随时提问。谢谢

import os 
import json
directory = 'C:/Users/HB35401/MAXDev/force-app/main/default/profiles' #folder containing profiles to be modified
os.chdir(directory)
newData = 'C:/testXMLBatch/additionalXML/addXML.xml' #xml file to append to profile-xml files.

for nameOfFile in os.listdir(directory): #for each profile in the directory
    if nameOfFile.endswith(".xml"):
        g = open(newData)
        data = g.read() #set the value of the newXML to the data variable
        f = open(nameOfFile)
        fileContent = f.read() #save the content of the profile to fileContent
        if data in fileContent:
            print('ERROR: XML is already inside the Profile.' + nameOfFile)
        else:
            EndLine = fileContent[-11:] #save the </Profile> tag from the bottom of the file to EndLine variable.
            #print(EndLine)            # theEndLine will be appended back after we add our new XML.
            test = fileContent[:-11] #remove the </Profile> tag and write back to the profile the removal of the </Profile> tag
            with open(nameOfFile, "w") as w:
                w.write(test)
            with open(nameOfFile) as t:
                fileContent2 = t.read()
                #print(fileContent2)   

            h = open(nameOfFile, "a") #add the new data to the profile along with the </Profile> tag
            h.write(data + "\n"+ EndLine)
            h.close()

Tags: 文件thetobranchfalsetruefieldqueue
1条回答
网友
1楼 · 发布于 2024-05-14 01:18:09

试试这个

from simplified_scrapy import SimplifiedDoc, utils
xml = utils.getFileContent('your xml file.xml')
doc = SimplifiedDoc(xml)
root = doc.Profile
nodes = root.children # Get all nodes

count = len(nodes)
if count:
    sorted_nodes = sorted(nodes, key=operator.itemgetter('tag')) # Sort by tag
    sorted_htmls = []
    for node in sorted_nodes:
        sorted_htmls.append(node.outerHtml) # Get the string of sorted nodes
    for i in range(0, count):
        nodes[i].repleaceSelf(sorted_htmls[i]) # Replace the nodes in the original text with the sorted nodes

print(doc.html)

相关问题 更多 >