How can I open and diplay the inhalt of  xml-file on a Jpanel

It sounds very simple to do that. I've already read the Xml-tutorial. But I still don't know, how can I do it?what is the best and simple way to do that? Any advice would be greatly appreciated! Please write some code for me, if possible. so that I can grasp it ASAP!

* Display_XML.java
import java.awt.*;
import java.io.*;
import javax.swing.*;
import javax.xml.parsers.*;
import org.w3c.dom.*;
public class Display_XML extends JFrame {
    public Display_XML() {
        initComponents();
        readXML();
    private void initComponents() {
        textScroller = new JScrollPane();
        textarea = new JTextArea();
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        setTitle("Alimentation:  product list");
        textarea.setEditable(false);
        textarea.setBackground(new Color(254,254,204));
        textScroller.setViewportView(textarea);
        getContentPane().add(textScroller, BorderLayout.CENTER);
        Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
        setBounds((screenSize.width-400)/2, (screenSize.height-300)/2, 400, 300);
This example reads this XML data file [Alimentaion_Test_File.xml]
<?xml version="1.0" encoding="ISO-8859-1"?>
<Alimentation>
     <Product>
          <Name>Milk</Name>
          <Price>1.50</Price>
     </Product>
     <Product>
          <Name>Cheese</Name>
          <Price>2.10</Price>
     </Product>
     <Product>
          <Name>Butter</Name>
          <Price>1.90</Price>
     </Product>
     <Product>
          <Name>Cream</Name>
          <Price>1.96</Price>
     </Product>
</Alimentation>
    private void readXML() {
        File file = new File("Alimentaion_Test_File.xml");
        try {
            DocumentBuilder builder =
                    DocumentBuilderFactory.newInstance().newDocumentBuilder();
            Document doc = builder.parse(file);
            NodeList nodes = doc.getElementsByTagName("Product");
            for (int i = 0; i < nodes.getLength(); i++) {
                Element element = (Element) nodes.item(i);
                NodeList title = element.getElementsByTagName("Name");
                Element line = (Element) title.item(0);
                textarea.append("Name: " + getCharacterDataFromElement(line)+"\n");
                NodeList url = element.getElementsByTagName("Price");
                line = (Element) url.item(0);
                textarea.append("   Price: " + getCharacterDataFromElement(line)+"\n");
        } catch (Exception e) {
            e.printStackTrace();
    public static String getCharacterDataFromElement(Element e) {
        Node child = e.getFirstChild();
        if (child instanceof CharacterData) {
            CharacterData cd = (CharacterData) child;
            return cd.getData();
        return "?";
    public static void main(String args[]) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                new Display_XML().setVisible(true);
    private JScrollPane textScroller;
    private JTextArea textarea;
}

Similar Messages

  • I've downloaded CS6 Master Collection but only see The Photoshop icon.  I can't seem to find the other programs, except on "uninstall programs" How do I open and run the others?

    I've downloaded CS6 Master Collection but only see The Photoshop icon.  I can't seem to find the other programs, except on "uninstall programs" How do I open and run the others?

    How about you give us some real information.
    Did you install the software? What operating system?

  • How can I open and listen to the PDF documents with audio in my iPad?

    How can I open and listen to the PDF documents with audio in my iPad?

    You need to use a PDF reader that supports multimedia. Adobe Reader 10.3 does not. Adobe has not stated whether it will support multimedia in the future.
    In the meantime, you could buy an application that does like PDF Expert. Read about it here:
    Finding the Best Tablet PDF Reader

  • How can I open and close private browsing using an IPAD mini in IOS 7?

    How can I open and close private browsing using an iPad mini in ios7?

    Tap the '+' to the right of the URL field to open a new tab, and at the bottom left of the screen there should be a 'Private' button - tap that and you can then go to the site that you want to go to

  • How can I open and print a unix E....le file

    how can I open and print a unix E...le file

    We have no idea what you're referring to. If you know how to use Terminal and the file has been compiled then it may run.

  • How can i open and edit microsoft documents in macbook pro

    how can i open and edit microsoft documents in macbook pro with retina display? I am a new user to mac book

    You could install Office for Mac. You could install a free Office-like program such as LibreOffice. You could open the documents with a program like Pages. Or you could install Windows and Office on your Macbook Pro.

  • How can I open and print PDF files?

    How can I open and print PDF files?

    With the free Reader if they are not print protected. With Acrobat otherwise.

  • How can I open and fill out a Secured Adobe PDF job application?

    How can I open and fill out a Secured Adobe PDF job application?

    Hi margueritew68053277,
    You should be able to fill that out using the free Adobe Reader, or Adobe Fill & Sign.
    Best,
    Sara

  • How can i open iphoto after the new update OS X 10.9.2 (13C64) i keep getting this mac can not open this application.

    How can i open iphoto after the new update OS X 10.9.2 (13C64) i keep getting this mac can not open this application.

    This screenshow shows which previous versions of iPhoto are compatible with Mavericks.  It also indicates which versions qualify for a free upgrade to iPhoto 9.5.1 and which require a purchase:
    OT

  • How can I open and edit files that were developed and published by ftp to my host on my old macbook, which I can no longer access.

    How can I open and edit files that were developed and published via ftp to my host on my old macbook, which I can no longer access. The HD containing the files cannot be accessed. I want to be able to download the published files to be edited in iweb?

    You CAN'T download published files and then edit them in iWeb because iWeb can't open them - it has no import facility so is unable to open any html or css files - a previously published site.
    You can download these files yes, but you'll need to use them as a guide or to cut and paste text from there whilst you re-build your site in iWeb.
    The only other way is to download the files and then open them with an html editor such as TextWrangler or use one of the other web design programmes out there that is capable of importing html and css such as Dreamweaver of Flux 4.
    These are your only alternatives as iWeb can't import and if you want to continue to use iWeb, then you'll have to re-build your site from scratch.  If you do just remember to back up your domain.sites file this time around - it is found under your User/Library/Applicaion Support/iWeb/domain.sites.

  • I have multiple iPhoto libraries w/ duplicates in several. How can I consolidate and find the dupes? Some photos are versions of the original.

    I have multiple iPhoto libraries w/ duplicates in several. How can I consolidate and find the dupes? Some photos are versions of the original. On my iMac, I also have many folders containing some of the same photos. My computer is used for both personal and business purposes. My libraries should be separated, one for each.

    To merge libraries use iPhoto Library Manager - http://www.fatcatsoftware.com/iplm/ -  or Aperature
    To find duplicates within an iPhoto library use iPhoto Library Manager - http://www.fatcatsoftware.com/iplm/ - 
    Photos that are not in an iPhoto library can be imported telling iPhoto not to import duplicates (an imperfect procews) and then IPLM can find the duplicates
    As to keeping seperate libraries - only you can do that - IPLM can move photos between libraries for you but only you can know which photos should be in which library
    LN

  • App store tried to update iBooks app. I can only see Newstand app on my screen and cannot get to bookstore. How can I delete and download the iBooks app again. iBook still appears in the settings menu as version 2.1   Tx iPad 2, iOS 5.0.1, iBooks 2.1

    App store tried to update iBooks app. I can only see Newstand app on my screen and cannot get to bookstore. How can I delete and download the iBooks app again. iBook still appears in the settings menu as version 2.1
    Tx
    iPad 2, iOS 5.0.1, iBooks 2.1

    Hi,
    the settings are gone, i guess. But even worse, I cannot get the iPad2 to play Full HD thru the Apple digital AV adaptor using apps that support  (and advertise this), like AVPlayer HD. This definiteley used to work before one of the last iOS upgrades. (I am now at 5.1.1).

  • How can I select and delete the fill color (white background) of a live trace (B&W) with in an actio

    How can I select and delete the fill color (white background) of a live trace (B&W) with in an action set?
    Illustrator CS4 in windows XP.

    Maybe Li[ve trace] is not t]he way[ to go
    I have some suggestion one leave it as is and use a blending mode of multiply to give it a color background like this
    leave it as is make it a grayscale tiff import int Illustrator and color it it in Illustrator like this
    Or trace over it with the brush tool aand then give it a color ground.
    Or do the original over and do a cleaner job with no tone or a minimum of tone. Then Live trace.

  • How can I resize and reposition the Date Created window for All windows?

    When setting view options for a finder window set to list view, I cannot get column resizing or positioning to hold. When making those changes, the option for "All Windows" automatically shifts to "This window only" and it then does not hold.
    How can I resize and reposition the Date Created column for All WIndows?

    Hi Harmz,
    Try dragging this file to the Îesktop & reboot...
    /Users/YourUserName/Library/Preferences/com.apple.finder.plist

  • How can I purchase and download the standalone LR6 here at Hong Kong?  There's no such option for me instead of forcing me to use the CC!

    How can I purchase and download the standalone LR6 here at Hong Kong?  There's no such option for me instead of forcing me to use the CC!

    Try this link:
    http://www.adobe.com/products/catalog.html
    Otherwise I suggest you contact your local office for help:
    Hong Kong office
    Suite 4102, 41/F
    The Lee Gardens
    33 Hysan Avenue
    Causeway Bay
    Hong Kong
    Tel: +852 2916 2100
    Fax: +852 2970 2277

Maybe you are looking for

  • Error in SXMB_MONI -"HTTP_RESP_STATUS_CODE_NOT_OK

    Hi all, In a file to file to scenario, i'm getting this error in SXMB_MONI Error - " HTTP_RESP_STATUS_CODE_NOT_OK"             503             Service Unavailable Please provide me the exact solution Thanks, Younus

  • How can I make my book accessible on a iPhone?

    I'm creating a book that I would like readers to be able to view on their iPhones? Is there a way I can do this within IBA? If not, what's the best way to go about it? Thanks.

  • Config to email using output determination

    Hi, I'm trying to set up output determination that will email a PDF of the invoice at the time of invoice creation.  I've created a communication strategy with a single entry for communication method "INT" for email.  I added an entry for simple mail

  • [SOLVED] Heavy CPU load for no apparent reason

    After my laptop is left on for about 15 minutes or so, with KDE 4.1 and a few programs running (firefox,a few okulars,dolphin,pidgin,emacs,conky), the load on my laptop climbs to over 4. 'top' shows X using 73% cpu, and about 10~15 kio_thumbnail proc

  • Start up- none - screen with a circle with a diagonal line through it

    what should i do? computer mac book pro 17 inch not starting up. circle with a line though it.