用StaX忽略评论

时间:2018-07-31 08:27:28

标签: xml stax xml-comments

使用以下代码,我已成功读取XML文件。但是,当注释出现在节点中间时,阅读器将丢弃该节点的其余部分。例如:

def rentacar_booking(request):
    if request.POST:
        template = Template.objects.get(template_default__exact=1)
        template_page = template.template_alias + str("/rentacar/rentacar_booking_form.html")
        menu_config_list = MenuItemRentacarList.objects.all()[0]
        menu_config = MenuItemRentacarList.objects.get(id=menu_config_list.id)
        args['main_menu'] = MenuMenu.objects.get(id__exact=template.template_main_menu_id)
        args['menu_items'] = MenuItem.objects.filter(
            menu_item_menu=args['main_menu'],
            menu_item_published=1,
        )
        args['current_menu_item'] = menu_config.menu_item_rentacar_list_menu_item
        all_modules = Module.objects.filter(
            module_show_option__exact='all',
            module_published=1
        )
        selected_modules = Module.objects.filter(
            module_show_option__exact='selected',
            module_published=1,
            module_menu_item=args['current_menu_item']
        )
        excluded_modules = Module.objects.filter(
            module_show_option__exact='except',
            module_published=1,
        ).exclude(
            module_menu_item=args['current_menu_item']
        )
        args['modules'] = list(chain(all_modules, selected_modules, excluded_modules))
        try:
            args['car'] = Car.objects.get(id__exact=request.POST.get('car_id'))
        except:
            args['car'] = None

        if args['car'] is not None:
            return render(request, template_page, args)
        else:
            return HttpResponseRedirect('/rentacar/list/')
    else:
        return HttpResponseRedirect('/')


def rentacar_booking_review(request):
    if request.POST:
        menu_config_list = MenuItemRentacarList.objects.all()[0]
        menu_config = MenuItemRentacarList.objects.get(id=menu_config_list.id)
        template = Template.objects.get(template_default__exact=1)
        template_page = template.template_alias + str("/rentacar/rentacar_booking_review.html")
        args['main_menu'] = MenuMenu.objects.get(id__exact=template.template_main_menu_id)
        args['menu_items'] = MenuItem.objects.filter(
            menu_item_menu=args['main_menu'],
            menu_item_published=1,
        )
        args['current_menu_item'] = menu_config.menu_item_rentacar_list_menu_item
        all_modules = Module.objects.filter(
            module_show_option__exact='all',
            module_published=1
        )
        selected_modules = Module.objects.filter(
            module_show_option__exact='selected',
            module_published=1,
            module_menu_item=args['current_menu_item']
        )
        excluded_modules = Module.objects.filter(
            module_show_option__exact='except',
            module_published=1,
        ).exclude(
            module_menu_item=args['current_menu_item']
        )
        args['modules'] = list(chain(all_modules, selected_modules, excluded_modules))

        book_customer_name = request.POST.get('book_customer_name')
        book_customer_lastname = request.POST.get('book_customer_lastname')
        book_customer_age = request.POST.get('book_customer_age')
        book_customer_idno = request.POST.get('book_customer_idno')
        book_customer_license_expire = request.POST.get('book_customer_license_expire')
        book_customer_phone = request.POST.get('book_customer_phone')
        book_customer_email = request.POST.get('book_customer_email')
        book_booking_start_date = request.POST.get('book_booking_start_date')
        book_booking_end_date = request.POST.get('book_booking_end_date')
        book_booking_car_car = request.POST.get('book_booking_car_car')
        booking_save_form = BookingApprovalForm(request.POST)
#       subject = "Thank you for joining Joinbuggy"
#       from_email = settings.EMAIL_HOST_USER
#       to_email = form.cleaned_data.get('email')
#       join_message = """Your booking is placed we will contact you later """
#       send_mail(subject=subject, from_email=from_email, recipient_list=[to_email], message=join_message,
#                 fail_silently=False)

        date_format = "%Y-%m-%d"
        a = datetime.strptime(book_booking_start_date, date_format)
        b = datetime.strptime(book_booking_end_date, date_format)
        days_delta = b - a

        if days_delta.days >= 1:
            car_price = Car.objects.get(id__exact=book_booking_car_car)
            total_price = int(car_price.car_price) * int(days_delta.days)

            if booking_save_form.is_valid():
                booking_approval = booking_save_form.save(commit=False)
                booking_approval.book_customer_name = book_customer_name
                booking_approval.book_customer_lastname = book_customer_lastname
                booking_approval.book_customer_age = book_customer_age
                booking_approval.book_customer_idno = book_customer_idno
                booking_approval.book_customer_license_expire = book_customer_license_expire
                booking_approval.book_customer_phone = book_customer_phone
                booking_approval.book_customer_email = book_customer_email
                booking_approval.book_booking_start_date = book_booking_start_date
                booking_approval.book_booking_end_date = book_booking_end_date
                booking_approval.book_booking_car_car_id = book_booking_car_car
                booking_approval.book_booking_total_price = total_price
                booking_approval.save()
                args['booking'] = booking_approval
                return render(request, template_page, args)
            else:
                return HttpResponseRedirect('/')
        else:
            return HttpResponseRedirect('/')
    else:
        return HttpResponseRedirect('/')


def rentacar_booking_approve(request):
    args = {}
    if request.POST:
        uuid = request.POST.get('book_unique_id')
        clear_uuid = uuid.replace('-', '')

        template = Template.objects.get(template_default__exact=1)
        template_page = template.template_alias + str("/rentacar/rentacar_booking_success.html")
        menu_config_list = MenuItemRentacarList.objects.all()[0]
        menu_config = MenuItemRentacarList.objects.get(id=menu_config_list.id)
        args['main_menu'] = MenuMenu.objects.get(id__exact=template.template_main_menu_id)
        args['menu_items'] = MenuItem.objects.filter(
            menu_item_menu=args['main_menu'],
            menu_item_published=1,
        )
        args['current_menu_item'] = menu_config.menu_item_rentacar_list_menu_item
        all_modules = Module.objects.filter(
            module_show_option__exact='all',
            module_published=1
        )
        selected_modules = Module.objects.filter(
            module_show_option__exact='selected',
            module_published=1,
            module_menu_item=args['current_menu_item']
        )
        excluded_modules = Module.objects.filter(
            module_show_option__exact='except',
            module_published=1,
        ).exclude(
            module_menu_item=args['current_menu_item']
        )
        args['modules'] = list(chain(all_modules, selected_modules, excluded_modules))

        try:
            prebook = BookingApproval.objects.get(book_unique_id=request.POST.get('book_unique_id'))
        except:
            prebook = None
        if prebook is not None:
            customer_save_form = CustomerForm(request.POST)
            if customer_save_form.is_valid():
                customer = customer_save_form.save(commit=False)
                customer.customer_firstname = prebook.book_customer_name
                customer.customer_lastname = prebook.book_customer_lastname
                customer.customer_age = prebook.book_customer_age
                customer.customer_idno = prebook.book_customer_idno
                customer.customer_license_expire = prebook.book_customer_license_expire
                customer.customer_phone = prebook.book_customer_phone
                customer.customer_email = prebook.book_customer_email
                customer.save()
                try:
                    saved_customer = Customer.objects.get(id=customer.id)
                except:
                    saved_customer = None
                if saved_customer is not None:
                    booking_save_form = BookingForm(request.POST)
                    if booking_save_form.is_valid():
                        booking = booking_save_form.save(commit=False)
                        booking.booking_car_car = prebook.book_booking_car_car  # Complete all fields for Booking model (except approved) and sir for car? ok because its in prebook . and car is foregn key in it . right sir
                        booking.booking_customer_customer = customer  # taking customer that we saved all his details, sure it's a foreign key!
                        booking.booking_start_date = prebook.book_booking_start_date
                        booking.booking_end_date = prebook.book_booking_end_date
                        booking.booking_total_price = prebook.book_booking_total_price
                        booking.save()
                        try:
                            booking = Booking.objects.get(id=booking.id)
                        except:
                            booking = None
                        if booking is not None:
                            args['car'] = car = Car.objects.get(id=prebook.book_booking_car_car.id)
                            car.car_available = 0
                            car.save()
                            prebook.delete()
                            return render(request, template_page, args)  # Template which says that booking successful
                        else:
                            return HttpResponseRedirect('/')  # Thd response page, that says that form is incorrect
                    else:
                        return HttpResponseRedirect('/')  # Thd response page, that says that form is incorrect
                else:
                    return HttpResponseRedirect('/')  # Thd response page, that says that form is incorrect
            else:
                return HttpResponseRedirect('/')  # Thd response page, that says that form is incorrect
        else:
            return HttpResponseRedirect('/')  # Thd response page, that says that form is incorrect
    else:
        return HttpResponseRedirect('/')

将导致返回字符串“ thisismy”,而没有其他结果。

当我遇到特殊字符如并将<text>thisismy<!--comment-->document</text> 设置为XMLInputFactory时,我遇到了类似的问题。我猜我遇到了一个相关功能。

我需要能够优雅地处理此类文档。谁能建议我如何解决此类干扰?

isCoalescing=true

0 个答案:

没有答案