如何使用DOM获取具有某些属性的节点数

时间:2013-01-22 13:10:41

标签: java dom xml-parsing

我有一个任务是在Eclipse中使用JDom解析XML文件。当我开始创建代码时,我的代码仅足以获得具有Tag名称模型的长度(多少)节点。任务更具体,我需要获取具有属性类型的标签名称模型的节点的长度。我应该如何修改我的代码以获得结果?

这是我的XML文件的一部分

<container>
  <models>
    <model id="FM1" type="BoQ">
      <meta
       <phase phaseDesc="PRCR>SLCT>"/>
    <domain domainCode="SPM.BOQ.RFP "/>
    <levelOfDetail levelOfDetailCode="[4]"/>
  </meta>
</model>

<model id="FM2" type="Object">
  <meta>
    <phase phaseDesc="PRCR>SLCT> "/>
    <domain domainCode="BIM"/>
    <levelOfDetail levelOfDetailCode="[4] "/>
  </meta>
</model>

<model id="FM3">
  <meta>   
    <phase phaseDesc="PRCR>SLCT>"/>
    <domain domainCode="SPM.QTO"/>
    <levelOfDetail levelOfDetailCode="[5]"/>
  </meta>
</model>

<model id="FM4" type="BoQ">
  <meta>   
    <phase phaseDesc="PRCR>SLCT>"/>
    <domain domainCode="TSM.TSC"/>
    <levelOfDetail levelOfDetailCode="[3]"/>
  </meta>
  </model>
 </models>

  <linkModels>
    <linkModel id="LM1" type="QuantitySplit">
  <meta>
    <info>
      <i k="ModelName" v="Linkmodell"/>
    </info>
    <domain domainCode="LKM.QSP" domainDesc="Link Model"/>
  </meta>
  <models>
    <model id="FM1"/>
    <model id="FM2"/>
    <model id="FM3"/>
    <model id="FM4"/>
  </models>

</container>

public class xmldom {

public static void main(String[] args) {

    Document xmlDoc = getDocument("./src/MMT_Angebot_Anfrage.xml");

    System.out.println("Root: "+ 
            xmlDoc.getDocumentElement().getNodeName());

    NodeList model = xmlDoc.getElementsByTagName("model");



    System.out.println("Number of Models " +
            model.getLength());

    String elementPhase = "phase";
    String elementDomain = "domain";
    String elementLOD = "levelOfDetail";
    String attrPhase = "phaseCode";
    String attrDomain = "domainCode";
    String attrLOD = "levelOfDetailCode";

    getElementAndAttrib(model, elementPhase, elementDomain, elementLOD, attrPhase, attrDomain, attrLOD); 
}

private static Document getDocument(String docString) {

    try {

        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();

        factory.setIgnoringComments(true);
        factory.setIgnoringElementContentWhitespace(true);
        factory.setValidating(true);

        DocumentBuilder builder = factory.newDocumentBuilder();

        return builder.parse(new InputSource(docString));

    }

    catch(Exception ex) {

        System.out.println(ex.getMessage());

    }


    return null;
}

private static void getElementAndAttrib(NodeList model,
        String elementPhase, String elementDomain, String elementLOD,
        String attrPhase, String attrDomain, String attrLOD) {

    try {

        for(int i=0; i < model.getLength(); i++){

            Node modelNode = model.item(i);

            Element modelElement = (Element)modelNode;


            NodeList phaseList = modelElement.getElementsByTagName(elementPhase);

            NodeList domainList = modelElement.getElementsByTagName(elementDomain);

            NodeList lodList = modelElement.getElementsByTagName(elementLOD);

            Element phaseElement = (Element)phaseList.item(0);

            Element domainElement = (Element)domainList.item(0);

            Element lodElement = (Element)lodList.item(0);

            NodeList elementList = phaseElement.getChildNodes();


                System.out.println("=================================================");               
                System.out.println(attrPhase + " : "+ phaseElement.getAttribute(attrPhase));
                System.out.println(attrDomain + " : "+ domainElement.getAttribute(attrDomain));
                System.out.println(attrLOD + " : "+ lodElement.getAttribute(attrLOD));

        }
    }

    catch(Exception ex) {

        System.out.println(ex.getMessage());

    }

  }
}

1 个答案:

答案 0 :(得分:1)

您想要计算包含type属性的<model>个元素的数量吗?如果,那么XPath是最合适的工具。

XPath xp = XPathFactory.newInstance().newXPath();

Double num = xp.evaluate("count(//model[@type])", doc, XPathConstants.NUMBER);

顺便说一下,你使用的是DOM,而不是JDOM。