2009-08-04 26 views
0

私はSTAFとSTAXで作業しています。 Pythonはコーディングに使用されます。私はPythonの初心者です。 私の仕事は、Document Factory Parserを使ってPythonでXMLファイルを解析することです。XML文書ビルダーファクトリを使用したPythonでの構文解析

私が解析しようとしているXMLファイルです:私はすべてのタグを読み取る必要が

<?xml version="1.0" encoding="utf-8"?> 
<operating_system> 
    <unix_80sp1> 
    <tests type="quick_sanity_test"> 
     <prerequisitescript>preparequicksanityscript</prerequisitescript> 
     <acbuildpath>acbuildpath</acbuildpath> 
     <testsuitscript>test quick sanity script</testsuitscript> 
     <testdir>quick sanity dir</testdir> 
    </tests> 
    <machine_name>u80sp1_L004</machine_name> 
    <machine_name>u80sp1_L005</machine_name> 
    <machine_name>xyz.pxy.dxe.cde</machine_name> 
    <vmware id="155.35.3.55">144.35.3.90</vmware> 
    <vmware id="155.35.3.56">144.35.3.91</vmware> 
    </unix_80sp1> 
</operating_system> 
  1. タグmachine_nameについては、リストに読み込む必要があります。 すべてのマシン名がリストmachnameである必要があります。 タグを読み取った後、machnameは[u80sp1_L004、u80sp1_L005、xyz.pxy.dxe.cde]にする必要があります。 すべての属性が= [155.35.3.55,155.35.3.56] すべてのVMwareの値は= [144.35.3.90,155.35.3.56]

vmware_valueでなければなりませんvmware_attrする必要があります。私はまた、すべてのVMwareタグを必要とする

  • 私はvmwareタグとマシン名タグ以外のすべてのタグを正しく読むことができます: 私は次のコードを使用しています:(私はxmlとvmwareを初めて使っています)。

    以下のコードを変更する必要があります。

    factory = DocumentBuilderFactory.newInstance(); 
    factory.setValidating(1) 
    factory.setIgnoringElementContentWhitespace(0) 
    builder = factory.newDocumentBuilder() 
    document = builder.parse(xmlFileName) 
    
    vmware_value = None 
    vmware_attr = None 
    machname = None 
    
    # Get the text value for the element with tag name "vmware" 
    nodeList = document.getElementsByTagName("vmware") 
    for i in range(nodeList.getLength()): 
    node = nodeList.item(i) 
    if node.getNodeType() == Node.ELEMENT_NODE: 
    children = node.getChildNodes() 
    for j in range(children.getLength()): 
    thisChild = children.item(j) 
    if (thisChild.getNodeType() == Node.TEXT_NODE): 
    vmware_value = thisChild.getNodeValue() 
    vmware_attr ==??? what method to use ? 
    # Get the text value for the element with tag name "machine_name" 
    nodeList = document.getElementsByTagName("machine_name") 
    for i in range(nodeList.getLength()): 
    node = nodeList.item(i) 
    if node.getNodeType() == Node.ELEMENT_NODE: 
    children = node.getChildNodes() 
    for j in range(children.getLength()): 
    thisChild = children.item(j) 
    if (thisChild.getNodeType() == Node.TEXT_NODE): 
    machname = thisChild.getNodeValue() 
    

    タグが存在するかどうかを確認する方法もあります。私は適切に構文解析をコード化する必要があります。

  • +0

    私はスペースがPythonでは重要であることを知っていますので、どのようにコードの壁をフォーマットするのか分かりません。あなたはそれであなた自身の上にいる、OP。 – Welbog

    答えて

    0

    あなたがいない文字列として、その代わりに、このリストとしてvmware_value、vmware_attrとmachnameをインスタンス化する必要があります:リストに項目を追加するために、次に

    vmware_value = [] 
    vmware_attr = [] 
    machname = [] 
    

    vmware_value = None 
    vmware_attr = None 
    machname = None 
    

    は、これを行いますあなたのリストにappendメソッドを使用してください。例えば:

    factory = DocumentBuilderFactory.newInstance(); 
    factory.setValidating(1) 
    factory.setIgnoringElementContentWhitespace(0) 
    builder = factory.newDocumentBuilder() 
    document = builder.parse(xmlFileName) 
    
    vmware_value = [] 
    vmware_attr = [] 
    machname = [] 
    
    # Get the text value for the element with tag name "vmware" 
    nodeList = document.getElementsByTagName("vmware") 
    for i in range(nodeList.getLength()): 
        node = nodeList.item(i) 
        vmware_attr.append(node.attributes["id"].value) 
        if node.getNodeType() == Node.ELEMENT_NODE: 
         children = node.getChildNodes() 
         for j in range(children.getLength()): 
          thisChild = children.item(j) 
          if (thisChild.getNodeType() == Node.TEXT_NODE): 
           vmware_value.append(thisChild.getNodeValue()) 
    

    また、私はvmware_attrとvmware_valueし、正しい値を追加するために動作するはずだと思う何かにコードを編集しました。

    私は、STAXがxml.domの構文を使用することを前提にしなければならなかったので、そうでない場合は、私の提案を適切に編集する必要があります。

    関連する問題