Jump to content

xml parsing and weird nodes

- - - - -

  • Please log in to reply
No replies to this topic

#1
denarced

denarced

    Programmer

  • Members
  • PipPipPipPip
  • 182 posts
Hey,
I'm trying a little xml on Java.
I'm not new to xml, been doing on c#.
so I made this little program which goes through the chosen xml looking for "levy"-nodes and stuff under em.
I don't understand where the parser finds the extra nodes.
it just says "#text" on those nodes.
MyFrame.java:
import java.awt.Container;

import java.awt.FlowLayout;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import java.io.File;


import javax.swing.JButton;

import javax.swing.JFileChooser;

import javax.swing.JFrame;

import javax.swing.JOptionPane;

import javax.swing.JTextArea;

import javax.xml.parsers.DocumentBuilder;

import javax.xml.parsers.DocumentBuilderFactory;


import org.w3c.dom.Document;

import org.w3c.dom.Element;

import org.w3c.dom.Node;

import org.w3c.dom.NodeList;



public class MyFrame extends JFrame implements ActionListener

{

	private JFileChooser fileCh;

	private JButton button;

	private JButton btnOpenFChooser;

	private JTextArea txtArea;

	private File file;

	public MyFrame()

	{

		super();

		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

		this.setSize(600, 400);

		this.setTitle("yritystä");

		Container cp = this.getContentPane();

		cp.setLayout(new FlowLayout());

		

		fileCh = new JFileChooser(new File("/home/"));

		

		btnOpenFChooser = new JButton("FC auki");

		cp.add(btnOpenFChooser);

		btnOpenFChooser.addActionListener(this);

		

		button = new JButton("ok");

		cp.add(button);

		button.addActionListener(this);

		

		txtArea = new JTextArea(15,20);

		cp.add(txtArea);

		

		this.setVisible(true);

	}

	public void actionPerformed(ActionEvent e)

	{

		if(e.getSource() == this.btnOpenFChooser)

		{

			int retVal = fileCh.showDialog(this, "OK");

			if(retVal == JFileChooser.APPROVE_OPTION)

				file = fileCh.getSelectedFile();

		}

		if(e.getSource() == this.button)

		{

			if(this.file != null)

				doXml();

		}

	}

	private void doXml()

	{

		try

		{

			DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();

			DocumentBuilder parser = factory.newDocumentBuilder();

			Document doc = parser.parse(file);

			NodeList list = doc.getElementsByTagName("levy");

			String str = "";

			for(int i=0;i<list.getLength();i++)

			{

				Node nod = list.item(i);

				str += nod.getNodeName();

				str += ":\n";

				NodeList l = nod.getChildNodes();

				for(int j=0;j<l.getLength();j++)

				{

					Node node = l.item(j);

					str+="\t"+node.getNodeName()+": ";

					str+=node.getTextContent()+"\n";

				}

			}

			txtArea.setText(str);

		}

		catch(Exception e)

		{

			JOptionPane.showMessageDialog(this,"virhe: "+e.getMessage()+"\n");

		}

	}

}

Main.java:
public class Main

{

	public static void main(String[] args)

	{

		new MyFrame();

	}

}
data.xml:
<?xml version="1.0" encoding="UTF-8" ?>

<levyt>

	<levy>

		<artisti>Samael</artisti>

		<title>Ceremony of Opposites</title>

		<vuosi>1994</vuosi>

	</levy>

	<levy>

		<artisti>Joe Satriani</artisti>

		<title>Beautiful Strange Music</title>

		<vuosi>2003</vuosi>

	</levy>

</levyt>
and my output in txtArea looks like:
levy:

	#text: 

		

	artisti: Samael

	#text: 

		

	title: Ceremony of Opposites

	#text: 

		

	vuosi: 1994

	#text: 

	

levy:

	#text: 

		

	artisti: Joe Satriani

	#text: 

		

	title: Beautiful Strange Music

	#text: 

		

	vuosi: 2003

	#text: 

	
The "FC auki"-button opens up the filechooser and "ok"starts the parsing..




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users