如何列出节点文本&使用Java和XPath的属性?

时间:2011-12-03 05:42:44

标签: java xml xslt xpath

以下是我收到的XML文件 -

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

我希望输出为 -

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

以下是我的Java代码 -

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

我无法获得相同的输出。我正在尝试使用XPATH和Java。虽然我粘贴了一个简单的Java程序,但我尝试了许多XPath查询的组合。但是无法获得所需的输出。我使用的是JDK 1.7。

任何帮助请...

1 个答案:

答案 0 :(得分:2)

好问题,+ 1。

使用XSLT 更容易(只需从代码中调用XSLT转换并输出结果):

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

在提供的XML文档上进行此转换时

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

生成所需的正确输出

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