0

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

这是我的 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>

公共类 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());

    }

  }
}
4

1 回答 1

1

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

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

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

顺便说一句,您使用的是 DOM,而不是 JDOM。

于 2013-01-23T10:30:33.720 回答