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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T05:45:19+00:00 2026-05-27T05:45:19+00:00

Below is the XML file, I am receiving – <Seminars> <Seminar> <Venue P=ABC dt=20111223/>

  • 0

Below is the XML file, I am receiving –

<Seminars>
  <Seminar>
    <Venue P="ABC" dt="20111223"/>
    <Subject name="Finance">
     <Topic main="Bonds"/>
     <Topic main="Stocks" sub="Technical Analysis"/>
   </Subject>       
  </Seminar>    
  <Seminar>
   <Venue P="ABC" dt="20111225"/>
   <Subject name="Yoga">
     <Topic main="Benefits"/>
   </Subject>
   <Subject name="Meditation">
     <Topic main="Benefits"/>
   </Subject>
  </Seminar>
  <Seminar>
   <Venue P="PQR" dt="20111220"/>       
   <Subject name="IT">
     <Topic main="Java" sub="Swing"/>
     <Topic main="Java" sub="NIO"/>
   </Subject>       
  </Seminar>
  <Seminar>
   <Venue P="ABC" dt="20111224"/>
   <Subject name="Medical">
     <Topic main="Plastic Surgery"/>
     <Topic main="Mal-nutrition"/>
   </Subject>
   <Subject name="IT">
     <Topic main="Java" sub="Collections"/>
     <Topic main="Web Technologies"/>
   </Subject>      </Seminar>
  <Seminar>     
   <Venue P="XYZ" dt="20111226"/>
   <Subject name="IT">
     <Topic main="DotNET - I"/>
     <Topic main="DotNET - II"/>
     <Topic main="XML" sub="Security"/>
   </Subject>       
  </Seminar>
  <Seminar>
   <Venue P="XYZ" dt="20111227"/>
   <Subject name="IT">
     <Topic main="Oracle"/>
     <Topic main="Oracle" sub="JDeveloper"/>
   </Subject>       
  </Seminar>
</Seminars>

I want the output as –

Financial
  - Bonds
  - Stocks, Technical Analysis
Yoga
  - Benefits
Meditation  
  - Benefits
IT
  - Java, Swing
  - Java, NIO
Medical
  - Plastic Surgery
  - Mal-nutrition
IT
  - Java, Collections
  - Web Technologies
IT
  - Java - I
  - Java - II
  - XML, Security
IT
  - Oracle, JDeveloper
  - Oracle, Security

Below is my Java code –

public class Seminar
{
    public static void main(String[] args) throws ParserConfigurationException, SAXException, IOException, XPathExpressionException 
    {
       DocumentBuilderFactory domFactory = DocumentBuilderFactory.newInstance();
       domFactory.setNamespaceAware(true); 
       DocumentBuilder builder = domFactory.newDocumentBuilder();
       Document doc = builder.parse("seminar.xml");
       XPath xpath = XPathFactory.newInstance().newXPath();

       // XPath Query for showing all nodes value
       XPathExpression expr = xpath.compile("//Seminars/Seminar");
       Object result = expr.evaluate(doc, XPathConstants.NODESET);
       NodeList nodes = (NodeList) result;

       //Writing to an output file
       String tx = "";
       BufferedWriter out = new BufferedWriter(new FileWriter("seminar.csv"));

       Node node;

       for (int i = 0; i < nodes.getLength(); i++) 
       {
          node = nodes.item(i);
          String subject = xpath.evaluate("Subject/@name",node);
          String t = xpath.evaluate("Subject/Topic/@main",node);
          String del = xpath.evaluate("Subject/Topic/@sub",node).toString();
          //Writing to the CSV File
          out.write(subject + "," + t + "," + del + "\r\n"); // + "   -   " + dod );
       }        
       out.close();
    }
 }

I am unable to get the same kinda output. I am trying to use XPATH and Java. I tried lot a many combinations of XPath query, though I am pasting a simple Java program. But was not able to get the required output. I am using JDK 1.7.

Any help please…

  • 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-05-27T05:45:19+00:00Added an answer on May 27, 2026 at 5:45 am

    Good question, +1.

    This is much easier to do with XSLT (just invoke the XSLT transformation from your code and output its result):

    <xsl:stylesheet version="1.0"
     xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
     <xsl:output method="text"/>
     <xsl:strip-space elements="*"/>
    
     <xsl:template match="Subject">
       <xsl:value-of select="concat('&#xA;',@name)"/>
       <xsl:apply-templates/>
     </xsl:template>
    
     <xsl:template match="Topic">
      <xsl:text>&#xA;  - </xsl:text>
      <xsl:apply-templates select="@*"/>
     </xsl:template>
    
     <xsl:template match="@sub">
      <xsl:value-of select="concat(', ', .)"/>
     </xsl:template>
    </xsl:stylesheet>
    

    When this transformation is apllied on the provided XML document:

    <Seminars>
        <Seminar>
            <Venue P="ABC" dt="20111223"/>
            <Subject name="Finance">
                <Topic main="Bonds"/>
                <Topic main="Stocks" sub="Technical Analysis"/>
            </Subject>
        </Seminar>
        <Seminar>
            <Venue P="ABC" dt="20111225"/>
            <Subject name="Yoga">
                <Topic main="Benefits"/>
            </Subject>
            <Subject name="Meditation">
                <Topic main="Benefits"/>
            </Subject>
        </Seminar>
        <Seminar>
            <Venue P="PQR" dt="20111220"/>
            <Subject name="IT">
                <Topic main="Java" sub="Swing"/>
                <Topic main="Java" sub="NIO"/>
            </Subject>
        </Seminar>
        <Seminar>
            <Venue P="ABC" dt="20111224"/>
            <Subject name="Medical">
                <Topic main="Plastic Surgery"/>
                <Topic main="Mal-nutrition"/>
            </Subject>
            <Subject name="IT">
                <Topic main="Java" sub="Collections"/>
                <Topic main="Web Technologies"/>
            </Subject>
        </Seminar>
        <Seminar>
            <Venue P="XYZ" dt="20111226"/>
            <Subject name="IT">
                <Topic main="DotNET - I"/>
                <Topic main="DotNET - II"/>
                <Topic main="XML" sub="Security"/>
            </Subject>
        </Seminar>
        <Seminar>
            <Venue P="XYZ" dt="20111227"/>
            <Subject name="IT">
                <Topic main="Oracle"/>
                <Topic main="Oracle" sub="JDeveloper"/>
            </Subject>
        </Seminar>
    </Seminars>
    

    the wanted, correct output is produced:

    Finance
      - Bonds
      - Stocks, Technical Analysis
    Yoga
      - Benefits
    Meditation
      - Benefits
    IT
      - Java, Swing
      - Java, NIO
    Medical
      - Plastic Surgery
      - Mal-nutrition
    IT
      - Java, Collections
      - Web Technologies
    IT
      - DotNET - I
      - DotNET - II
      - XML, Security
    IT
      - Oracle
      - Oracle, JDeveloper
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Below is the XML file - <Seminars> <Seminar> <Venue P=ABC dt=20111223/> <Subject name=Finance> <Topic
Below is my XML File - <CVs> <CV> <Name>ABC</Name> <Address></Address> <Introduction></Introduction> <CompSkills>Java, XSLT, XPATH,
I'm traversing an xml file as below: <?xml version=1.0 encoding=ISO-8859-1?> <wrapper> <site base=http://www.example1.co.uk/ name=example
I am trying to read the below xml file and get the name-value pairs
One of my application is generating below XML file. <root> <command name=Set> <property name=PWR.WakeupOnLAN
below is the xml file - <Countries> <Country> <Name>India</Name> <Capital>New Delhi</Capital> </Country> <Country> <Name>USA</Name>
i have 2 xml file as below: staff.xml <staff> <person> <PID>1</PID> <name>John</name> </person> <person>
Below is the XML file - <Continents> <Continent n=Asia> <Country n=Thailand> <City> <Name>Bangkok</Name> <Desc>Capital
How to read the below xml file and write its content into a plain
How do I change my XML file below to have CAS return to the

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.