gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 14:54:18 26 4
gpt4 key购买 nike

我有一个任务是在 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>

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

}

}
}

最佳答案

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

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

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

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

关于java - 如何使用DOM获取具有某些属性的节点的数量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14459351/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com