package test1;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.io.StringReader;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathFactory;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Tree;
import org.eclipse.swt.widgets.TreeItem;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;
public class test1 {
public static void main(String[] args) {
// TODO Auto-generated method stub
Display display = new Display();
Shell shell = new Shell(display);
shell.setText("TEXT EXAMPLE");
shell.setBounds(100, 100, 200, 100);
// Composite composite = new Composite(shell,SWT.BORDER);
// composite.setBounds(25,25,150,250);
FillLayout fillLayout = new FillLayout(SWT.HORIZONTAL);
shell.setLayout(fillLayout);
final Combo combo1 = new Combo(shell, SWT.READ_ONLY);
final Tree tree = new Tree(shell, SWT.SINGLE);
combo1.setBounds(25, 25, 100, 75);
combo1.setItems(new String[] { "first", "second", "third" });
combo1.setText("First");
// final Label label = new Label(shell, SWT.CENTER);
combo1.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent event) {
// label.setText("Selected : " + combo1.getText());
try {
// File xmlDoc = new File("NewFile.xml");
/*
BufferedReader br = new BufferedReader(
new InputStreamReader(new FileInputStream("C://workspace//test1/NewFile.xml"), "UTF8"));
StringBuilder sb = new StringBuilder();
while (true) {
String line = br.readLine();
if (line == null)
break;
sb.append(line);
}
String xml = sb.toString();
br.close();
*/
String xml =
"<class>"
+"<grandparent named='g1'>"
+"<parent name='p1'>"
+"<child name='c1'>child1</child>"
+"<child name='c1'>child2</child>"
+"<child name='c1'>child3</child>"
+"</parent>"
+"<parent name='p1'>"
+"<child name='c2'>child1</child>"
+"<child name='c2'>child2</child>"
+"<child name='c2'>child3</child>"
+"</parent>"
+"</grandparent>"
+"<grandparent named='g2'>"
+"<parent name='p2'>"
+"<child name='c3'>child1</child>"
+"<child name='c3'>child2</child>"
+"<child name='c3'>child3</child>"
+"</parent>"
+"<parent name='p2'>"
+"<child name='c4'>child1</child>"
+"<child name='c4'>child2</child>"
+"<child name='c4'>child3</child>"
+"</parent>"
+"</grandparent>"
+"</class>";
InputSource is = new InputSource(new StringReader(xml));
Document document = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(is);
XPath xpath = XPathFactory.newInstance().newXPath();
//NodeList cols = (NodeList) xpath.evaluate("//grandparent/parent", document, XPathConstants.NODESET);
/*
* for (int idx = 0; idx < cols.getLength(); idx++) {
* System.out.println(cols.item(idx).getNodeName());
*
* } System.out.println(cols.getLength());
*/
// id 가 c2 인 Node의 val attribute 값 가져오기
// Node col2 = (Node) xpath.evaluate("//*[@id='c2']", document,
// XPathConstants.NODE);
// System.out.println(col2.getAttributes().getNamedItem("val").getTextContent());
/*
* DocumentBuilderFactory dbFact = DocumentBuilderFactory.newInstance();
* DocumentBuilder dBuild = dbFact.newDocumentBuilder(); Document doc =
* dBuild.parse(xmlDoc);
*/
// System.out.println("Root element : " +
// doc.getDocumentElement().getNodeName());
NodeList nList1 = (NodeList) xpath.evaluate("//grandparent", document, XPathConstants.NODESET);
NodeList nList2 = (NodeList) xpath.evaluate("//*[@name='p1']", document, XPathConstants.NODESET);
NodeList nList3 = (NodeList) xpath.evaluate("//*[@name='p1']/child[@name='c1']", document, XPathConstants.NODESET);
for (int i = 0; i < nList1.getLength(); i++) {
// NodeList nList = (NodeList)
// xpath.evaluate("//grandparent[@idno='g1']/parent/child", document,
// XPathConstants.NODESET);
// Node nNode = nList.item(i);
TreeItem grandParent1 = new TreeItem(tree, 0);
grandParent1.setText(nList1.item(i).getNodeName());
for (int j = 0; j < nList2.getLength(); j++) {
// Node nNode2 = nList2.item(j);
TreeItem parent1 = new TreeItem(grandParent1, 0);
parent1.setText(nList2.item(j).getNodeName());
for (int k = 0; k < nList3.getLength(); k++) {
// Node nNode3 = nList3.item(k);
TreeItem child1 = new TreeItem(parent1, 0);
// parent1.setText(eElement.getElementsByTagName("parent").item(0).getTextContent());
// parent1.setText(eElement.getElementsByTagName("parent").item(1).getTextContent());
child1.setText(nList3.item(k).getTextContent());
// System.out.println("student id : " + eElement.getAttribute("idno"));
// System.out.println("student first name : "
// + eElement.getElementsByTagName("parent").item(0).getTextContent());
}
}
}
} catch (Exception e) {
System.out.println("error");
}
/*
* for (int i = 0; i < 4; i++) { TreeItem grandParent = new TreeItem(tree, 0);
* grandParent.setText(nNode.getNodeName()); for (int j = 0; j < 4; j++) {
* TreeItem parent = new TreeItem(grandParent, 0); parent.setText("Parent -" +
* j); for (int k = 0; k < 4; k++) { TreeItem child = new TreeItem(parent, 0);
* child.setText("Child -" + k); } } }
*/
}
});
/*
* tree.addSelectionListener(new SelectionAdapter(){ public void
* widgetSelected(SelectionEvent event){ TreeItem[] selected =
* tree.getSelection(); if(selected.length>0){
* System.out.println("Selected: "+selected[0].getText()); } } });
*/
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch())
display.sleep();
}
display.dispose();
}
}
'JAVA > JAVA(MEC)' 카테고리의 다른 글
grid data (0) | 2020.08.24 |
---|---|
oc 명령어 전체정리 (0) | 2020.08.14 |
eclipse rcp version 정리 (0) | 2020.08.14 |
combo상자에 xml파일 파싱 (0) | 2020.08.14 |
openshift template 제거(administer로 로그인) (0) | 2020.08.14 |