Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
import org.w3c.dom.*;
import javax.xml.parsers.*;

public class Main {
    public static void test() {
        String xml = "C:\\Users\\is96092\\Desktop\\music\\test.xml";
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        try {
            DocumentBuilder db = dbf.newDocumentBuilder();
            Document dom = db.parse(xml);
            Element doc = dom.getDocumentElement();
            String role1 = "";
            role1 = getTextValue(role1, doc, "note");
            System.out.println("result : " + role1);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private static String getTextValue(String def, Element doc, String tag) {
        String value = def;
        NodeList noteList = doc.getElementsByTagName(tag);
        if (noteList.getLength() > 0) {
            for (int i = 0; i < noteList.getLength(); i++) {
                Node note = noteList.item(i);

                Element noteElement = (Element) note;
                if (noteElement.getNodeType() == Node.ELEMENT_NODE) {
                    if (noteElement.getElementsByTagName("pitch").getLength() > 0) {
                        Element pitchElement = (Element) noteElement.getElementsByTagName("pitch").item(0);
                        System.out.println("Staff idX : "
                                + pitchElement.getElementsByTagName("step").item(0).getTextContent());
                    } else {
                        System.out.println("yoktir");
                    }
                    //               System.out.println("First Name : " + eElement.getElementsByTagName("firstname").item(0).getTextContent());
                    //               System.out.println("Last Name : " + eElement.getElementsByTagName("lastname").item(0).getTextContent());                 
                } else {
                    System.out.println("not element");
                }
            }

            //          value = nl.item(0).getFirstChild().getNodeValue();
        }
        return value;
    }
}