2013-12-17 57 views
0

這裏我的XML文件和我的XML文件的大小大於100MB .............如何通過Java讀取XML的父屬性值(如果其子屬性值匹配)

<?xml version="1.0" encoding="UTF-8"?> 
    <products inbound.code.version="" schema.version="" xslt.version="0" hierarchy.type="provisioner" hierarchy.remap="emea" hierarchy.language="en" hierarchy.country="eu" hierarchy.translation="en" hierarchy.depth="product" remap="product" depth="0"> 
    <product.type name="myprod" translation="myprod" change="modified" oid="84609819" oid.2="84609819" common.oid="3709945" remap="category" depth="1"> 
    <category name="myprod Adapters" translation="myprod Adapters" change="modified" oid="84609824" oid.2="84609824" common.oid="3710111" remap="category" depth="2"> 
    <sub.category name="myprod Ethernet Adapters" translation="myprod Ethernet Adapters" change="modified" oid="81995406" oid.2="81995406" common.oid="1844132" remap="category" depth="3"> 
    <big.series name="Nova myprod Ethernet Adapters" translation="Nova myprod Ethernet Adapters" change="modified" oid="81996409" oid.2="81996409" common.oid="496741" remap="category" depth="4"> 
    <small.series name="Nova myprod Dual NC370i Multifunction Network Adapter" translation="Nova myprod Dual NC370i Multifunction Network Adapter" change="modified" oid="81997928" oid.2="81997928" common.oid="496742" remap="category" depth="5"> 
    <model name="Nova BL20p G3 Dual NC370i Multifunction Network Adapter" translation="Nova BL20p G3 Dual NC370i Multifunction Network Adapter" change="modified" oid="82020916" oid.2="82020916" common.oid="496746" remap="category" depth="6"> 
    <product name="Nova BL20p G3 Dual NC370i Multifunction Network Adapter" translation="Nova BL20p G3 Dual NC370i Multifunction Network Adapter" number="product1" change="modified" oid="64857725" oid.2="64857725" common.oid="496743" remap="product" depth="7"/> 
    </model> 
    <model name="Nova BL20p G3 Dual NC370i Multifunction Network Adapter" translation="Nova BL20p G3 Dual NC370i Multifunction Network Adapter" change="modified" oid="82020916" oid.2="82020916" common.oid="496746" remap="category" depth="6"> 
    <product name="Nova BL20p G3 Dual NC370i Multifunction Network Adapter" translation="Nova BL20p G3 Dual NC370i Multifunction Network Adapter" number="product2" change="modified" oid="64857725" oid.2="64857725" common.oid="496743" remap="product" depth="7"/> 
    </model> 
     </small.series> 
     </big.series> 
     </sub.category> 
     </category> 
    </product.type> 
    </Nova.products> 

我必須輸入一個產品編號爲通過Java搜索參數,如果該產品的編號一致「通過Java 輸入的產品數量在深度=」 = 7" ,那麼我必須讓common.oid屬性值爲depth =「6」

回答

0

嘗試使用sax解析器解析此xml文檔(推薦用於只讀);

覆蓋startElement(...) - 方法:

  • 如果起始元素是候選人,則將其存儲在變量中;
  • 如果起始元素符合您的需要,您可以引用父項;

覆蓋所述的endElement(..) - 方法

  • 如果(最終元件是候選(以類比的startElement)然後設置候選空

這個片段可能。求助:

private Object candidate; //you must define your own kind of candidate! 
public void findIt(){ //stupid name, you can do better 
    try { 
     SAXParserFactory factory = SAXParserFactory.newInstance(); 
     SAXParser saxParser = factory.newSAXParser(); 

     DefaultHandler handler = new DefaultHandler() { 

      @Override 
      public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException { 

       if (qName.equalsIgnoreCase(candidateTagName)){ 
        candidate = new Object(); 
        //populate the candidate with required infos 
        //use attributes.getValue(attributeName).trim(); 
        //this is your parent on level n 
       } 

       if (qName.equalsIgnoreCase(childTagName)){ 
        //now you found your child at level n+1 
        //and you can refer to parent at level n: 
        candidate.getSomething(); 
        //or do something 
        //or even return something (requires method return value change) 
       } 
      } 

      @Override 
      public void endElement(String arg0, String arg1, String arg2) throws SAXException { 
       super.endElement(arg0, arg1, arg2); 

       if (qName.equalsIgnoreCase(candidateTagName)){ 
        candidate = null; 
       } 

      } 
     } 
    }; 

    saxParser.parse(xmlFile, handler); 

    } catch (ParserConfigurationException e) { 
     e.printStackTrace(); 
    } catch (SAXException e) { 
     e.printStackTrace(); 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } 
} 

很好 - 你的候選對象必須定義,你可以比我做得更好!並定義您的標籤

+0

感謝您的回覆..... – user3109713