python-3.x 如何使用xsdata库创建CDATA节?

bzzcjhmw  于 2023-05-02  发布在  Python
关注(0)|答案(1)|浏览(91)

我正在创建与Finn API对话的服务,需要XML而不是JSON。基于 *。dtd文件和xsdata库我生成了一些类,这些类允许我构建一个请求对象,然后将其序列化为XML。我遇到的问题是一些字段应该用CDATA节 Package ,如:我不知道如何正确地做这件事。我想到的一个非常简单的想法是在生成XML之后使用replace方法。
xml.replace(data['company_url'], f"<![CDATA[{data['company_url']}]]>")
但我想知道什么是最好的解决方案,这种情况下:)提前感谢!

from finn.xml.dtd.request.iadif_job79 import (
    IadIfJob,
    Head,
    Object,
    ObjectHead,
    ObjectLocation,
    Orderno,
    Job,
    ApplicationAddress,
    Company,
    JobContact,
    GeneralTextJob,
    Occupation,
    OverwriteMmo,
    OverwriteMmoModus,
    Moreinfo,
)
from xsdata.formats.dataclass.serializers import XmlSerializer
from xsdata.formats.dataclass.serializers.config import SerializerConfig

def create_finn_request(
        partner: str,
        provider: str,
        street_address: str,
        zip_code: str,
        country_code: str,
        order_no: str,
        from_date: str,
        to_date: str,
        heading: str,
        job_title: str,
        sector: str,
        duration: str,
        salary: str,
        company_name: str,
        company_url: str,
        company_ingress: str,
        job_contact_name: str,
        job_contact_title: str,
        job_contact_phone: str,
        job_contact_email: str,
        job_contact_linkedin_url: str,
        job_contact_twitter: str,
        application_deadline: str,
        application_url: str,
        no_of_positions: str,
        video_url: str,
        occupation_general: str,
        occupation_specialization: str,
        industry: str,
        general_heading: str = '',
        general_text: str = '',
        application_address: str = '',
        user_reference: str = '',
        provider_reference: str = '',
        version_no: str = '',
        company_facebook_url: str = '',
        company_linkedin_url: str = '',
        company_twitter: str = ''
) -> IadIfJob:
    head = Head(
        partner=partner,
        provider=provider
    )
    object_location = ObjectLocation(
        streetaddress=street_address,
        zipcode=zip_code,
        countrycode=country_code
    )
    order_no = Orderno(value=order_no)
    modus = OverwriteMmoModus(value=OverwriteMmoModus.ALL)
    overwrite_mmo = OverwriteMmo(modus=modus)
    object_head = ObjectHead(
        orderno=order_no,
        user_reference=user_reference,
        provider_reference=provider_reference,
        versionno=version_no,
        overwrite_mmo=overwrite_mmo,
        fromdate=from_date,
        todate=to_date,
        object_location=object_location,
        heading=heading,
    )
    application_address = ApplicationAddress(address=application_address)
    company = Company(
        company_name=company_name,
        url=company_url,
        ingress=company_ingress,
        facebook_url=company_facebook_url,
        linkedin_url=company_linkedin_url,
        twitter=company_twitter
    )
    general_text_job = GeneralTextJob(
        general_heading=general_heading,
        general_text=general_text
    )
    job_contact = JobContact(
        name=job_contact_name,
        title=job_contact_title,
        phone=job_contact_phone,
        email=job_contact_email,
        linkedin_url=job_contact_linkedin_url,
        twitter=job_contact_twitter,
    )
    more_info = Moreinfo(
        url=application_url,
    )
    occupation = Occupation(
        general=occupation_general,
        specialization=occupation_specialization,
    )
    job = Job(
        job_title=job_title,
        sector=sector,
        duration=duration,
        salary=salary,
        application_address=[application_address],
        company=[company],
        general_text_job=[general_text_job],
        application_deadline=application_deadline,
        job_contact=[job_contact],
        moreinfo=[more_info],
        no_of_positions=no_of_positions,
        video_url=video_url,
        application_url=application_url,
        occupation=[occupation],
        industry=[industry]
    )

    object_ = Object(
        object_head=object_head,
        job=job
    )

    root = IadIfJob(head=head, object_value=[object_])

    return root

if __name__ == "__main__":
    data = dict(
        partner='partner_id',
        provider='provider',
        order_no='991518d0-0d25-4faf-86f6-f6c00b37176c',
        from_date='01.05.2023',
        to_date='01.06.2023',
        street_address='streetname',
        zip_code='3745',
        country_code='no',
        heading='Join us!',
        job_title='Python Developer',
        sector='PRIVATE',
        duration='PERMAMENT',
        salary='15000',
        company_name='Company Name',
        company_url='https://workingexmaple.no',
        company_ingress='<p>THis is us</p>',
        general_text='<p>Join us bla bla bla</p>',
        application_deadline='01.06.2023',
        job_contact_name='Krystian Jarmuł',
        job_contact_title='',
        job_contact_phone='+48123456789',
        job_contact_email='krystian.jarmul@gmail.com',
        job_contact_linkedin_url='',
        job_contact_twitter='',
        application_url='https://applicationurl.com',
        no_of_positions='1',
        video_url='',
        occupation_general='OCCUPATION_ITDEVELOP',
        occupation_specialization='OCCUPATION_ITDEVELOP_AI',
        industry='INDUSTRY_IT'
    )
    finn_request = create_finn_request(**data)

    config = SerializerConfig(pretty_print=True)
    serializer = XmlSerializer(config=config)
    xml = serializer.render(finn_request)

生成的xml:

<?xml version="1.0" encoding="UTF-8"?>
<IAD.IF.JOB>
  <HEAD>
    <PARTNER>partner_id</PARTNER>
    <PROVIDER>provider</PROVIDER>
  </HEAD>
  <OBJECT>
    <OBJECT_HEAD>
      <ORDERNO PREVIEW="no">991518d0-0d25-4faf-86f6-f6c00b37176c</ORDERNO>
      <USER_REFERENCE/>
      <PROVIDER_REFERENCE/>
      <OVERWRITE_MMO MODUS="all"/>
      <VERSIONNO/>
      <FROMDATE>01.05.2023</FROMDATE>
      <TODATE>01.06.2023</TODATE>
      <OBJECT_LOCATION>
        <STREETADDRESS>streetname</STREETADDRESS>
        <ZIPCODE>3745</ZIPCODE>
        <COUNTRYCODE>no</COUNTRYCODE>
      </OBJECT_LOCATION>
      <HEADING>Join us!</HEADING>
    </OBJECT_HEAD>
    <JOB>
      <JOB_TITLE>Python Developer</JOB_TITLE>
      <SECTOR>PRIVATE</SECTOR>
      <DURATION>PERMAMENT</DURATION>
      <SALARY>15000</SALARY>
      <APPLICATION_ADDRESS>
        <ADDRESS/>
      </APPLICATION_ADDRESS>
      <COMPANY>
        <COMPANY_NAME>Company Name</COMPANY_NAME>
        <URL>https://workingexmaple.no</URL>
        <INGRESS>&lt;p&gt;THis is us&lt;/p&gt;</INGRESS>
        <FACEBOOK_URL/>
        <LINKEDIN_URL/>
        <TWITTER/>
      </COMPANY>
      <GENERAL_TEXT_JOB>
        <GENERAL_HEADING/>
        <GENERAL_TEXT>&lt;p&gt;Join us bla bla bla&lt;/p&gt;</GENERAL_TEXT>
      </GENERAL_TEXT_JOB>
      <APPLICATION_DEADLINE>01.06.2023</APPLICATION_DEADLINE>
      <JOB_CONTACT>
        <NAME>Krystian Jarmuł</NAME>
        <TITLE/>
        <PHONE>+48123456789</PHONE>
        <EMAIL>krystian.jarmul@gmail.com</EMAIL>
        <LINKEDIN_URL/>
        <TWITTER/>
      </JOB_CONTACT>
      <MOREINFO>
        <URL>https://applicationurl.com</URL>
      </MOREINFO>
      <NO_OF_POSITIONS>1</NO_OF_POSITIONS>
      <VIDEO_URL/>
      <APPLICATION_URL>https://applicationurl.com</APPLICATION_URL>
      <OCCUPATION>
        <GENERAL>OCCUPATION_ITDEVELOP</GENERAL>
        <SPECIALIZATION>OCCUPATION_ITDEVELOP_AI</SPECIALIZATION>
      </OCCUPATION>
      <INDUSTRY>INDUSTRY_IT</INDUSTRY>
    </JOB>
  </OBJECT>
</IAD.IF.JOB>

我试着用这个Hacky解决方案:xml.replace(data['company_url'], f"<![CDATA[{data['company_url']}]]>")但我觉得这不是最好的主意。另外,我在xsdata文档中搜索,没有找到序列化器中的任何参数,也没有找到如何处理CDATA,甚至没有提到CDATA。

8xiog9wr

8xiog9wr1#

为什么不使用python中的标准xml.etree.ElementTree

import xml.etree.ElementTree as ET

def cdata(tag_name, content):
    # Return etree object with tag name and CDATA content
    tag = tag_name.upper()
    cdata_strg = f"<![CDATA[{content}]]>"
    company_url = ET.Element(tag)
    company_url.text = cdata_strg
    return company_url

url = cdata("company_url","<URL>https://workingexmaple.no</URL>")
print(url)
print(url.tag, url.text)

输出:

<Element 'COMPANY_URL' at 0x00000193651CD170>
COMPANY_URL <![CDATA[<URL>https://workingexmaple.no</URL>]]>

相关问题