Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9178235
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T17:30:16+00:00 2026-06-17T17:30:16+00:00

I have a task to parse an XML file with JDom in Eclipse. When

  • 0

I have a task to parse an XML file with JDom in Eclipse. When I started to create the code, my code is only sufficient to get the length(how many) node with Tag name model. The task is more specific, I need to get the length of the node with tag name model which has attribute type. How should I modify my code to get the result?

This is part of my XML file

<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 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T17:30:17+00:00Added an answer on June 17, 2026 at 5:30 pm

    You want to count the number of <model> elements that contains the type attribute? If, so XPath is the most suitable tool.

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

    Btw, you are using DOM, not JDOM.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Im new in Java, and i have a task to Parse one xml file
I have sample code written for parsing xml file using javax.xml package. it uses
It's very simple task, but I struggle for hours. I have parse xml from
I have this code for reading XML files: try { File fXmlFile = new
I have a 1.6gb xml file, and when I parse it with Sax Machine
I have a big (1.9 GB) XML file which has data I want to
I'm writing a simple Perl script that uses XML::Smart to create and parse an
I am trying to parse the following XML file in Java and to store
As the title says it, I have a huge xml file (GBs) <root> <keep>
Guys I'm new to xml in Java. I have the following task. I need

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.