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 8818015
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T04:59:37+00:00 2026-06-14T04:59:37+00:00

Here is my xml code… <flow> <TaskID>100</TaskID> <TaskID>101</TaskID> <TaskID>102</TaskID> <TaskID>103</TaskID> </flow> I want to

  • 0

Here is my xml code…

<flow>
    <TaskID>100</TaskID>
    <TaskID>101</TaskID>
    <TaskID>102</TaskID>
    <TaskID>103</TaskID>    
</flow>

I want to know how to get taskID values in a for loop in java. Please help me…

  • 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-14T04:59:38+00:00Added an answer on June 14, 2026 at 4:59 am

    DOM parser solution, fairly simple, no extra libraries required.

    public static void main(String[] args) throws SAXException, IOException,
            ParserConfigurationException {
    
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        DocumentBuilder builder = factory.newDocumentBuilder();
    
        String input = "<outer>";
        input += "<otherstuff><TaskID>123</TaskID></otherstuff>";
        input += "<flow>";
        input += "<TaskID>100</TaskID>";
        input += "<TaskID>101</TaskID>";
        input += "<TaskID>102</TaskID>";
        input += "<TaskID>103</TaskID>";
        input += "</flow>";
        input += "</outer>";
        Document document = builder.parse(new InputSource(new StringReader(
                input)));
    
        NodeList flowList = document.getElementsByTagName("flow");
        for (int i = 0; i < flowList.getLength(); i++) {
            NodeList childList = flowList.item(i).getChildNodes();
            for (int j = 0; j < childList.getLength(); j++) {
                Node childNode = childList.item(j);
                if ("TaskID".equals(childNode.getNodeName())) {
                    System.out.println(childList.item(j).getTextContent()
                            .trim());
                }
            }
        }
    }
    

    You’d need to use a FileReader instead if your input came from a file.

    Document document = builder.parse(new InputSource(new FileReader(
            new File("foo.xml"))));
    

    An alternative to getElementsByTagName() is XPath, a query language for XML, this is particularly useful if you have complicated set of conditions to match.

    XPathFactory xPathfactory = XPathFactory.newInstance();
    XPath xpath = xPathfactory.newXPath();
    XPathExpression expr = xpath.compile("//flow/TaskID/text()");
    
    Object result = expr.evaluate(document, XPathConstants.NODESET);
    NodeList nodes = (NodeList) result;
    for (int i = 0; i < nodes.getLength(); i++) {
        System.out.println(nodes.item(i).getTextContent());
    }
    

    If your XML file is large, like 100s of MB / GB or you’re on a low memory platform then consider a SAX parser.

    String input = "<flow><TaskID>100</TaskID><TaskID>101</TaskID><TaskID>102</TaskID><TaskID>103</TaskID></flow>";
    SAXParser sax = SAXParserFactory.newInstance().newSAXParser();
    DefaultHandler handler = new DefaultHandler() {
        private StringBuilder buffer = new StringBuilder();
        @Override
        public void endElement(String uri, String localName, String qName)
                throws SAXException {
            if ("TaskID".equals(qName)) {
                System.out.println(buffer);
                buffer = new StringBuilder();
            }
        }
        @Override
        public void characters(char[] ch, int start, int length)
                throws SAXException {
            buffer.append(ch, start, length);
        }
        @Override
        public void startElement(String uri, String localName,
                String qName, Attributes attributes) throws SAXException {
            buffer = new StringBuilder();
        }
    };
    sax.parse(new InputSource(new StringReader(input)), handler);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

here is my xml code <autoLoad value=true/> <autoPlay value=false/> <playContinuously value=true/> <jumpToNextCategory value=false/> <loop
I am new to xml and android programming.Here is my xml code... I don't
My xml code is here, <?xml version=1.0 encoding=utf-8?><RelativeLayout xmlns:android=http://schemas.android.com/apk/res/android android:layout_width=fill_parent android:layout_height=fill_parent android:id=@+id/mainlayout > <LinearLayout
I'm trying to learn some XML Parsing here and I've been given some code
Here's the code I have: from cStringIO import StringIO from lxml import etree xml
Here is the code I have tried to append a node in existing xml,
Here is my code. <?xml version=1.0 encoding=utf-8?> <fx:Script> <![CDATA[ import comps.sampleTextArea; import mx.managers.PopUpManager; protected
enter code here I have a ListView that have a different Layout XML for
Here's my layout code; <?xml version=1.0 encoding=utf-8?> <LinearLayout xmlns:android=http://schemas.android.com/apk/res/android android:orientation=vertical android:layout_width=fill_parent android:layout_height=fill_parent> <TextView android:text=@string/welcome
Here is my code #!usr/bin/env perl # Setup includes use strict; use XML::RSS; use

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.