解析多个XML虽然并非所有文件都包含python元素

时间:2015-06-17 15:39:44

标签: python xml

这是我的代码,我有一些问题..

from xml.dom import minidom
openFiles = 'myxml/*.xml'

list = []

for xmlfiles in glob.glob(openFiles):
    doc = minidom.parse(xmlfiles)
    root = doc.getElementsByTagName("info")[0]
    project_name = root.getAttribute('project_name')
    list.append(project_name)
    ....

此代码正常运行。但不知何故,因为我跑来打开多个文件的错误。原因是因为并非所有文件都有' info'元素。那么有没有办法让它继续运行并将其更改为“没有”?

所以示例就像这样

project1,project2,none,project3,none

抱歉我的英文不好,并提前谢谢

1 个答案:

答案 0 :(得分:1)

您可以根据常见的Python编码原则EAFP(比请求更容易请求宽恕)添加$(document).ready(function(){ var modalObj = $('.confirm_modal').courses_modal_box(); $("#confirm").click(function() { if (v_changeInd === 'N') { $('.confirm_modal').courses_modal_box({ description: '<p>You have already submitted a request for a preferred first name of: ' + v_oldPFN + ' Please wait for the request to be completed (within 2 business days)</p>' + '<br><br>' + '<div style="text-align:center;">' + '<input type="button" value="Okay" class="courses_modal_close">' + '</div>' + '', width: '600', height: '300' });//Second modal 处理,如果XML中没有(function($){ // Defining our jQuery plugin $.fn.courses_modal_box = function(prop){ // Default parameters var options = $.extend({ height : "300", width : "600", title:" Preferred First Name", description: "Preferred First Name", top: "50%", left: "50%" },prop); return this.click(function(e){ add_block_page(); add_popup_box(); add_styles(); $('.courses_modal_box').fadeIn(); }); function add_styles(){ $('.courses_modal_box').css({ 'width':'600px', 'height':'300px', 'position': 'absolute', //Keep as absolute //'top': '50%', //'left': '50%', //'top': '400px', //'left': '500px', 'top': '400', 'right': '400', 'bottom': '400', 'left': '400', //'display':'center', 'border':'1px solid #000000', 'box-shadow': '0px 2px 7px #292929', 'background': '#f2f2f2', 'vertical-align': 'center', '-moz-box-shadow': '0px 2px 7px #292929', '-webkit-box-shadow': '0px 2px 7px #292929', '-ms-box-shadow': '0px 2px 7px #292929', 'border-radius':'10px', '-border-radius':'10px', '-moz-border-radius':'10px', '-webkit-border-radius':'10px', 'z-index':'50' }); $('.courses_modal_close').css({ 'position':'relative', 'top':'-45px', 'left':'20px', 'float':'center', 'display':'block', 'text-align':'center', //Things I adjusted for the okay button on my modal. 'height':'50px', 'width':'100px', 'vertical-align': 'middle', // 'border':'2px solid #000000', 'margin':'0 auto' //Centers the button in fireFox }); /*Block page overlay*/ var pageHeight = $(document).height(); var pageWidth = $(window).width(); $('.courses_block_page').css({ 'position':'absolute', 'top':'0', 'left':'0', //'background-color':'rgba(0,0,8,0.5)', 'height':pageHeight, 'width':pageWidth, 'z-index':'10' }); $('.courses_inner_modal_box').css({ 'position':'initial', 'background-color':'#FFF', 'height':(options.height - 70) + 'px', 'width':(options.width - 60) + 'px', // 'padding':'10px', // 'margin-bottom':'-50px', 'margin':'1px', 'border-radius':'10px', //working in fireFox '-webkit-border-radius':'10px', //chrome '-border-radius':'10px', //IE 'vertical-align': 'middle', 'text-align': 'center' }); } function add_block_page(){ var block_page = $('<div class="courses_block_page"></div>'); $(block_page).appendTo('body'); } function add_popup_box(){ var pop_up = $('<div class="courses_modal_box"><a href="#" class="courses_modal_close"></a><div style="vertical-align:middle;" class="courses_inner_modal_box"><h2>' + options.title + '</h2><p>' + options.description + '</p></div></div>').css({height:"300px"}); $(pop_up).appendTo('.courses_block_page'); $('.courses_modal_close').click(function(){ $(this).parent().fadeOut().remove(); $('.courses_block_page').fadeOut().remove(); }); } return this; }; 元素,则将处理异常并{{ 1}}将被添加到列表中:

try...except

或者您可以使用LBYL(在跳跃之前查看)编码原则并在获取其属性之前检查'info'是否为XML:

None