Specialty Forums
Security and Virus
General Hardware
CPUs/Overclocking
Networking
Digital Photo/Video
Office Software
PC Gaming
Console Gaming
Programming
Database
Web Development
Digital Home

General Forums
Windows XP
Windows Vista
Windows 95/98
Windows Me
Windows NT
Windows 2000
Win Server 2008
Win Server 2003
Windows 3.1
Linux
PDAs
BeOS
Novell Netware
OpenVMS
Solaris
Disk Op. System
Unix
Mac
OS/2

Drivers
Driver Scan
Driver Forum

Software
Automatic Updates

BIOS Updates

My Computing.Net

Solution Center

Free IT eBook

Howtos

Site Search

Message Find

RSS Feeds

Install Guides

Data Recovery

About

Home
Reply to Message Icon Go to Main Page Icon

XML and Tree

Original Message
Name: seancky2
Date: April 25, 2008 at 14:07:58 Pacific
Subject: XML and Tree
OS: XP Pro
CPU/Ram: 64 939 3200
Comment:
I have a simple xml document. For my class, I need to create a "tree" out of the document. I have constructed a tree but i would like to double check the validity of my tree. Is there any simple program that will read an .xml file and construct a tree?

Report Offensive Message For Removal


Response Number 1
Name: seancky2
Date: April 25, 2008 at 14:10:21 Pacific
Subject: XML and Tree
Reply: (edit)
Here is the .xml schema that I used to create my tree that i would like to double check.

<?xml version="1.0" encoding="utf-8" ?>
- <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="fname" type="xs:string" />
<xs:element name="mname" type="xs:string" />
<xs:element name="lname" type="xs:string" />
<xs:element name="street" type="xs:string" />
<xs:element name="city" type="xs:string" />
<xs:element name="state" type="xs:string" />
<xs:element name="salary" type="xs:integer" />
<xs:element name="age" type="xs:integer" />
- <xs:element name="company">
- <xs:complexType>
- <xs:sequence>
- <xs:element name="project" minOccurs="0" maxOccurs="unbounded">
- <xs:complexType>
- <xs:sequence>
<xs:element name="name" type="xs:string" />
- <xs:element name="location" maxOccurs="4">
- <xs:complexType>
- <xs:sequence>
<xs:element ref="street" />
<xs:element ref="city" />
<xs:element ref="state" />
</xs:sequence>
</xs:complexType>
</xs:element>
- <xs:element name="participant" minOccurs="5" maxOccurs="20">
- <xs:complexType>
- <xs:sequence>
- <xs:element name="name">
- <xs:complexType>
- <xs:sequence>
<xs:element ref="fname" />
<xs:element ref="mname" minOccurs="0" maxOccurs="4" />
<xs:element ref="lname" />
</xs:sequence>
</xs:complexType>
</xs:element>
- <xs:element name="address" maxOccurs="4">
- <xs:complexType>
- <xs:sequence>
<xs:element ref="street" />
<xs:element ref="city" />
<xs:element ref="state" />
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element ref="age" />
<xs:element ref="salary" />
</xs:sequence>
</xs:complexType>
</xs:element>
- <xs:element name="leader">
- <xs:complexType>
- <xs:sequence>
- <xs:element name="name">
- <xs:complexType>
- <xs:sequence>
<xs:element ref="fname" />
<xs:element ref="mname" minOccurs="0" maxOccurs="4" />
<xs:element ref="lname" />
</xs:sequence>
</xs:complexType>
</xs:element>
- <xs:element name="address" maxOccurs="4">
- <xs:complexType>
- <xs:sequence>
<xs:element ref="street" />
<xs:element ref="city" />
<xs:element ref="state" />
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element ref="age" />
<xs:element ref="salary" />
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>


Report Offensive Follow Up For Removal

Response Number 2
Name: aha
Date: May 22, 2008 at 22:03:55 Pacific
Subject: XML and Tree
Reply: (edit)
Last semester I was in Uni, we learned to use SAX to
parse an XML file and create tree in java. Or you can
use DOM to do it if the file is not so huge. Let me try to find any code
from other sites.

Computer Programmer


Report Offensive Follow Up For Removal

Response Number 3
Name: aha
Date: May 22, 2008 at 22:06:33 Pacific
Subject: XML and Tree
Reply: (edit)
I copied following code from
http://www.java2s.com/Code/Java/XML...

----------------------
// XmlTreeViewerSax.java
----------------------

/*
* =============================================================================
* Copyright (c) 1998-2005 Jeffrey M. Hunter. All rights reserved.
*
* All source code and material located at the Internet address of
* http://www.idevelopment.info is the copyright of Jeffrey M. Hunter, 2005 and
* is protected under copyright laws of the United States. This source code may
* not be hosted on any other site without my express, prior, written
* permission. Application to host any of the material elsewhere can be made by
* contacting me at jhunter@idevelopment.info.
*
* I have made every effort and taken great care in making sure that the source
* code and other content included on my web site is technically accurate, but I
* disclaim any and all responsibility for any loss, damage or destruction of
* data or any other property which may arise from relying on it. I will in no
* case be liable for any monetary damages arising from such loss, damage or
* destruction.
*
* As with any code, ensure to test this code in a development environment
* before attempting to run it in production.
* =============================================================================
*/

// Core Java APIs
import java.io.IOException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

// SAX
import org.xml.sax.Attributes;
import org.xml.sax.ContentHandler;
import org.xml.sax.ErrorHandler;
import org.xml.sax.InputSource;
import org.xml.sax.Locator;
import org.xml.sax.SAXException;
import org.xml.sax.SAXParseException;
import org.xml.sax.XMLReader;
import org.xml.sax.helpers.XMLReaderFactory;

// Swing
import java.awt.*;
import javax.swing.*;
import javax.swing.tree.*;

/**
----------------------
* Application that allows users to view the contents of an XML do*****ent as
* a graphical tree. Also provides an example that demonstrates SAX events
* and associated callback methods that can be used to perform action within
* the parsing of an XML do*****ent.
*
* COMPILE:

* javac -classpath $JAVALIB/xercesImpl.jar XmlTreeViewerSax.java
*
* javac -classpath %JAVALIB%\xercesImpl.jar XmlTreeViewerSax.java
*
*
* RUN PROGRAM:

* java -classpath $JAVALIB/xercesImpl.jar:. XmlTreeViewerSax Tablespaces.xml
*
* java -classpath %JAVALIB%\xercesImpl.jar;. XmlTreeViewerSax Tablespaces.xml
*
----------------------
* @version 1.0
* @author Jeffrey M. Hunter (jhunter@idevelopment.info)
* @author http://www.idevelopment.info
----------------------
*/

public class XmlTreeViewerSax extends JFrame {


// Default parser to use
private String vendorParserClass =
"org.apache.xerces.parsers.SAXParser";

// The base tree to render
private JTree jTree;

// Tree Model to use
DefaultTreeModel defaultTreeModel;

public XmlTreeViewerSax() {
// Handle Swing setup
super("SAX Tree Viewer");
setSize(600, 450);
}


public void init(String xmlURI)
throws IOException, SAXException {

DefaultMutableTreeNode base = new DefaultMutableTreeNode(
"XML Do*****ent: " + xmlURI);

// Build Tree Model
defaultTreeModel = new DefaultTreeModel(base);
jTree = new JTree(defaultTreeModel);

// Construct the tree hierarchy
buildTree(defaultTreeModel, base, xmlURI);

// Display the results
getContentPane().add(new JScrollPane(jTree), BorderLayout.CENTER);

}


public void buildTree( DefaultTreeModel treeModel
, DefaultMutableTreeNode base
, String xmlURI)
throws IOException, SAXException {

// Create instances needed for parsing
XMLReader reader = XMLReaderFactory.createXMLReader(vendorParserClass);

// Register content handler

// Register error handler

// Parse
InputSource inputSource = new InputSource(xmlURI);
reader.parse(inputSource);

}


/**
* Sole entry point to the class and application.
* @param args Array of String arguments.
*/
public static void main(String[] args) {

if (args.length != 1) {
System.err.println(
"Usage: java XmlTreeViewerSax " +
"[XML Do*****ent URI]"
);
System.exit(1);
}

String do*****entURI = args[0];

try {
XmlTreeViewerSax viewer = new XmlTreeViewerSax();
viewer.init(do*****entURI);
viewer.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}

}

}

Computer Programmer


Report Offensive Follow Up For Removal

Response Number 4
Name: aha
Date: May 22, 2008 at 22:16:20 Pacific
Subject: XML and Tree
Reply: (edit)
It seems like not for your question.
Sorry.

Phil

Computer Programmer


Report Offensive Follow Up For Removal




Use following form to reply to current message:

   Name: From My Computing.Net Settings
 E-Mail: From My Computing.Net Settings

Subject: XML and Tree

Comments:

 
  Homepage URL (*): 
Homepage Title (*): 
         Image URL: 
 


Data Recovery Software




how to setup call of duty to joytok

WindowsME / HotMail Problem

Corrupt memory

Convert fat32 to Ntfs

Best WinMo phone of 2008


The information on Computing.Net is the opinions of its users. Such opinions may not be accurate and they are to be used at your own risk. Computing.Net cannot verify the validity of the statements made on this site. Computing.Net and Computing.Net, LLC hereby disclaim all responsibility and liability for the content of Computing.Net and its accuracy.
PLEASE READ THE FULL DISCLAIMER AND LEGAL TERMS BY CLICKING HERE

All content ©1996-2007 Computing.Net, LLC