How to get the full XML when decoding bytes to string. It is generating XML but truncated

227 Views Asked by At

I am using dicttoxml for generating an XML file in Python.

    **xml = dicttoxml(obj,  item_func = my_item_func, attr_type=False, custom_root='Benefits')**

and it generates a byte XML which I am again converting to string for my XSD validation.

    **strxml = bytes.decode(xml, 'utf-8')**

Issue : When the XML is generated it skips many nodes and are replaced by ..., the reason I think it is doing so because the XML file is very big. I don't want it to skip nodes I want the XML file in its entirety. However when this "xml" byte object is rendered on the browser or when I print in debug mode there is no issue and I get the XML in it's entirety. How can I overcome this problem?

Here is the complete code.

from datetime import date
from dicttoxml import dicttoxml
from xml.dom.minidom import parseString
import json
import xmlschema

def response_handler(self,node, mktsegtype):
    isvalidxml = False
    if node is not None:
        obj = json.loads(json.dumps(node,default=lambda o: dict((key, value) for key, value in o.__dict__.items() if value is not None),indent=4,allow_nan=False))
        my_item_func = lambda x: 'cvrg' if x == "Covered" else('Insure' if x == "Insurance" else  x[:-1])

        xml = dicttoxml(obj,  item_func = my_item_func, attr_type=False, custom_root='Benefits')
        isvalidxml = self.validatexmlwithxsd(xml, mktsegtype)
        if(isvalidxml):
            return xml
        else:
            return None
            
def validatexmlwithxsd(self, xml, mktsegtype):
    valid = False
    xsd = None
    strxml = bytes.decode(xml, 'utf-8')

    if(mktsegtype == "XXX"):
        xsd = xmlschema.XMLSchema('tests/test_cases/examples/vehicles/vehicles.xsd')
    elif(mktsegtype == "YYY"):
        xsd = xmlschema.XMLSchema('tests/test_cases/examples/vehicles/boat.xsd')        
    valid = xsd.is_valid(strxml)
return valid

E.g.of node generated

    <cvrg>
        <cvrgID>285</cvrgID>
        <cvrgCoveredRtl>1</cvrgCoveredRtl>
        <cvrgCoveredMail>1</cvrgCoveredMail>
        **<cvrgO...goryAgeLimitMax>**
        </cvrgAgeLimitMax>
        <cvrgOutOfLimitActionAge></cvrgOutOfLimitActionAge>
        <cvrgOutOfLimitActionGender></cvrgOutOfLimitActionGender>
    </cvrg>
    <cvrg>
        <cvrgID>559</cvrgID>
        <cvrgCoveredRtl>2</cvrgCoveredRtl>
        <cvrgCoveredMail>2</cvrgCoveredMail>
        <cvrgOutOfLimitActionAge></cvrgOutOfLimitActionAge>
        <cvrgOutOfLimitActionGender></cvrgOutOfLimitActionGender>
    </cvrg>

Update 1: - I tried to capture the print output to a variable, based on below url Python: Assign print output to a variable.

    s = StringIO()
    print(xml, file=s,flush=True)
    result = s.getvalue()

In this case also I getting a truncated XML nodes(with...) but as I mentioned in my original post when I print the byte object in the debug window or render it into the browser I am getting the entire XML. Any suggestion or help!!!

0

There are 0 best solutions below