如何处理xsdata库中的CDATA标签?

问题描述 投票:0回答:0

我正在努力创建与 Finn API 对话的服务,它需要 XML 而不是 JSON。 基于 *.dtd 文件和 xsdata 库,我生成了允许我构建请求对象然后将其序列化为 XML 的类。我遇到的问题是某些字段应该用 CDATA 部分包装,例如:

<URL><![CDATA[https://workingexmaple.no]]></URL>
我不确定如何以正确的方式进行。 我想到的一个 hacky 想法是在生成 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='[email protected]',
        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>[email protected]</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。

python python-3.x xml xml-serialization dtd
© www.soinside.com 2019 - 2024. All rights reserved.