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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T07:05:17+00:00 2026-05-27T07:05:17+00:00

Below is the XML file – <Seminars> <Seminar> <Venue P=ABC dt=20111223/> <Subject name=Finance> <Topic

  • 0

Below is the XML file –

<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>

Below is the Java program –

import org.w3c.dom.*;
import javax.xml.xpath.*;
import javax.xml.parsers.*;
import java.io.IOException;
import org.xml.sax.SAXException;

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();

      String qry = "//Seminars/Seminar[contains(Subject/@name,'Medical')]/Subject/Topic/@main";
      XPathExpression expr = xpath.compile(qry);
      Object result = expr.evaluate(doc, XPathConstants.NODESET);
      NodeList nodes = (NodeList) result;

      for (int i = 0; i < nodes.getLength(); i++) 
      {
           System.out.println(nodes.item(i).getNodeValue());
      }
   }
}

Using the above program I was expecting output as –

Plastic Surgery
Mal-nutrition

But I am also getting the output for the next Subject Tag which is having attribute value as IT …???

What am I missing in the query…?

  • 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-27T07:05:18+00:00Added an answer on May 27, 2026 at 7:05 am

    Your query should be

      String qry = "//Seminars/Seminar/Subject[contains(@name,'Medical')]/Topic/@main";
    

    You were asking for all the topics in the Seminar containing a Subject with “Medical” in the @name. The new query asks for all the Topics in the Subject that contains “Medical” in the @name. Subtle, but important difference!

    • 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, I am receiving - <Seminars> <Seminar> <Venue P=ABC dt=20111223/>
Below is my XML File - <School> <Std c=8> <Name>ABC</Name> <Age>12</Age> <Name>EFG</Name> <Age>11</Age> <Name>PQR</Name>
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
Below is my xml file called product.xml <products> <product> <name>Pen</name> <price>8</price> </product> <product> <name>Bag</name>
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>
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

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.