需要帮助从XML文件中提取子节点

时间:2017-05-23 18:12:24

标签: java xml xml-parsing

我需要一些从XML中提取子节点的帮助。我的XML文件如下所示,我想知道子节点" Stereotype"是否存在或note.by NodeList nChilds = nNode.getChildNodes(); 我确实得到子节点但无法找到子节点。 XML文件

    < Activity BacklogActivityId="0" BodyFontSize="0" Documentation_plain="" Id="xJmijDqGAqB8AQnX" Name="Activity" PmAuthor="mameh" PmCreateDateTime="2017-05-22T10:47:43.779" PmLastModified="2017-05-22T10:49:53.126" Postcondition="" Precondition="" QualityScore="-1" ReadOnly="false" Reentrant="false" SingleExecution="false" UserIDLastNumericValue="0">
< Stereotypes>
**Stereotype Idref="e5bijDqGAqB8AQuV" Name="Context_Activity"/>**
< /Stereotypes>
< MasterView>
< Activity Idref="3xmijDqGAqB8AQmn" Name="Activity"/>
< /MasterView>
< /Activity>

我的代码

    DocumentBuilderFactory dbFactory 
    = DocumentBuilderFactory.newInstance();
    DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
    Document doc = dBuilder.parse(inputFile);
    doc.getDocumentElement().normalize();
    System.out.println("Root element :" 
    + doc.getDocumentElement().getNodeName());
    NodeList nList = doc.getElementsByTagName("Activity");
    System.out.println("----------------------------");
    for (int temp = 0; temp < nList.getLength(); temp++) {
    Node nNode = nList.item(temp);
    NamedNodeMap att= nNode.getAttributes();
    if(att.getLength()==15)
    {
    **NodeList nChilds = nNode.getChildNodes();**
    System.out.println(nChilds.getLength());

1 个答案:

答案 0 :(得分:0)

可能是因为它认为文本是节点子项

相关问题