简体   繁体   中英

How to list the Node text & Attribute using Java and XPath?

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM