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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T11:09:38+00:00 2026-06-13T11:09:38+00:00

I am currently working on an academic project, developing in Java and XML .

  • 0

I am currently working on an academic project, developing in Java and XML. Actual task is to parse XML, passing required values preferably in HashMap for further processing. Here is the short snippet of actual XML.

<root>
  <BugReport ID = "1">
    <Title>"(495584) Firefox - search suggestions passes wrong previous result to form history"</Title>

    <Turn>
      <Date>'2009-06-14 18:55:25'</Date>
      <From>'Justin Dolske'</From>
      <Text>
        <Sentence ID = "3.1"> Created an attachment (id=383211) [details] Patch v.2</Sentence>
        <Sentence ID = "3.2"> Ah. So, there's a ._formHistoryResult in the....</Sentence>
        <Sentence ID = "3.3"> The simple fix it to just discard the service's form history result.</Sentence>
        <Sentence ID = "3.4"> Otherwise it's trying to use a old form history result that no longer applies for the search string.</Sentence>
      </Text>
    </Turn>

    <Turn>
      <Date>'2009-06-19 12:07:34'</Date>
      <From>'Gavin Sharp'</From>
      <Text>
        <Sentence ID = "4.1"> (From update of attachment 383211 [details])</Sentence>
        <Sentence ID = "4.2"> Perhaps we should rename one of them to _fhResult just to reduce confusion?</Sentence>
      </Text>
    </Turn>

    <Turn>
      <Date>'2009-06-19 13:17:56'</Date>
      <From>'Justin Dolske'</From>
      <Text>
        <Sentence ID = "5.1"> (In reply to comment #3)</Sentence>
        <Sentence ID = "5.2"> &amp;gt; (From update of attachment 383211 [details] [details])</Sentence> 
        <Sentence ID = "5.3"> &amp;gt; Perhaps we should rename one of them to _fhResult just to reduce confusion?</Sentence>
        <Sentence ID = "5.4"> Good point.</Sentence>
        <Sentence ID = "5.5"> I renamed the one in the wrapper to _formHistResult. </Sentence>
        <Sentence ID = "5.6"> fhResult seemed maybe a bit too short.</Sentence>
      </Text>
    </Turn>

  .....
  and so on
</BugReport>

There are many commenter like ‘Justin Dolske’ who have commented on this report and what I actually looking for is the list of commenter and all sentences they have written in a whole XML file. Something like if(from == justin dolske) getHisAllSentences(). Similarly for other commenters (for all). I have tried many different ways to get the sentences only for ‘Justin dolske’ or other commenters, even in a generic form for all using XPath, SAX and DOM but failed. I am quite new to these technologies including JAVA and any don’t know how to achieve it.

Can anyone guide me specifically how could I get it with any of above technologies or is there any other better strategy to do it?

(Note: Later I want to put it in a hashmap such as like this HashMap (key, value) where key = name of commenter (justin dolske) and value is (all sentences))

Urgent help will be highly appreciated.

  • 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-13T11:09:41+00:00Added an answer on June 13, 2026 at 11:09 am

    There’re several ways using which you can achieve your requirement.

    • One way would be use JAXB. There’re several tutorials available on this on the web, so feel free to refer to them.

    • You can also think of creating a DOM and then extracting data from it and then put it into your HashMap.

    One reference implementation would be something like this:

    import java.io.File;
    import java.util.ArrayList;
    import java.util.HashMap;
    
    import javax.xml.parsers.DocumentBuilderFactory;
    
    import org.w3c.dom.Document;
    import org.w3c.dom.Element;
    import org.w3c.dom.NodeList;
    
    public class XMLReader {
    
        private HashMap<String,ArrayList<String>> namesSentencesMap;
    
        public XMLReader() {
            namesSentencesMap = new HashMap<String, ArrayList<String>>();
        }
    
        private Document getDocument(String fileName){
            Document document = null;
    
            try{
                document = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(new File(fileName));
            }catch(Exception exe){
                //handle exception
            }
    
            return document;
        }
    
        private void buildNamesSentencesMap(Document document){
            if(document == null){
                return;
            }
    
            //Get each Turn block
            NodeList turnList = document.getElementsByTagName("Turn");
            String fromName = null;
    
            NodeList sentenceNodeList = null;
            for(int turnIndex = 0; turnIndex < turnList.getLength(); turnIndex++){
                Element turnElement = (Element)turnList.item(turnIndex);
                //Assumption: <From> element
                Element fromElement = (Element) turnElement.getElementsByTagName("From").item(0); 
                fromName = fromElement.getTextContent();
                //Extracting sentences - First check whether the map contains 
                //an ArrayList corresponding to the name. If yes, then use that,  
                //else create a new one                                              
                ArrayList<String> sentenceList = namesSentencesMap.get(fromName);
                if(sentenceList == null){
                    sentenceList = new ArrayList<String>();
                }
                //Extract sentences from the Turn node
                try{
                    sentenceNodeList = turnElement.getElementsByTagName("Sentence");
                    for(int sentenceIndex = 0; sentenceIndex < sentenceNodeList.getLength(); sentenceIndex++){
                        sentenceList.add(((Element)sentenceNodeList.item(sentenceIndex)).getTextContent());
                    }
                }finally{
                    sentenceNodeList = null;
                }
                //Put the list back in the map                  
                namesSentencesMap.put(fromName, sentenceList);
            }
        }
    
        public static void main(String[] args) {
            XMLReader reader = new XMLReader();
            reader.buildNamesSentencesMap(reader.getDocument("<your_xml_file>"));
    
            for(String names: reader.namesSentencesMap.keySet()){
                System.out.println("Name: "+names+"\tTotal Sentences: "+reader.namesSentencesMap.get(names).size());
            }
        }
    }
    

    Note: This is just a demonstration and you would need to modify it to suit your need. I’ve created it based on your XML to show one way of doing it.

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

Sidebar

Related Questions

I am currently working on an academic project which employs an ASTVisitor to create
Currently working on database part of android project. The main aim of the project
Currently working in Java, i'd like to be able to select part of an
I currently working on a project which uses Spree Cart and which has hence
im currently working on a project that involves listing contacts with a photo and
Im currently working on a project that requires the following. I need to be
I'm currently working on learning some game development with Java. I'm working on the
Currently working on a project with the cloud / azure and windows phone 7,
Im currently working on a c# project that uses another .net library. This library
Currently working on a flex AIR project based on PureMVC framework. There was a

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.