0

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!!!

4
  • Please provide a minimal reproducible example. Commented Jun 30, 2022 at 5:56
  • Thank you mzjn for responding. The question is edited now to include the code. Commented Jun 30, 2022 at 11:21
  • It sounds like you are viewing the repr of the XML, and this representation doesn't display the entire document. So all the nodes are really present in the object - that's why you can seem them in your browser - but the representation doesn't show them. Commented Aug 16, 2022 at 17:09
  • Thanks snakecharmerb for your response. Is there a possible way to see the entire XML!!!! Commented Aug 16, 2022 at 19:47

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.