简体   繁体   English

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

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

Below is the XML file, I am receiving - 下面是我收到的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>

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 - 下面是我的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();
    }
 }

I am unable to get the same kinda output. 我无法得到相同的输出。 I am trying to use XPATH and Java. 我正在尝试使用XPATH和Java。 I tried lot a many combinations of XPath query, though I am pasting a simple Java program. 虽然我粘贴了一个简单的Java程序,但我尝试了许多XPath查询的组合。 But was not able to get the required output. 但是无法获得所需的输出。 I am using JDK 1.7. 我使用的是JDK 1.7。

Any help please... 请帮忙...

Good question, +1. 好问题,+ 1。

This is much easier to do with XSLT (just invoke the XSLT transformation from your code and output its result): 使用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>

When this transformation is apllied on the provided XML document : 在提供的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>

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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