Ниже приведен 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. Я перепробовал множество комбинаций запросов XPath, хотя я вставляю простую Java-программу Но не смог получить требуемый результат. Я использую JDK 1.7.
Любая помощь, пожалуйста ...