Computing.Net > Forums > Database > XML and Tree

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

XML and Tree

Reply to Message Icon

Name: seancky2
Date: April 25, 2008 at 14:07:58 Pacific
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?



Sponsored Link
Ads by Google

Response Number 1
Name: seancky2
Date: April 25, 2008 at 14:10:21 Pacific
Reply:

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>


0

Response Number 2
Name: aha
Date: May 22, 2008 at 22:03:55 Pacific
Reply:

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


0

Response Number 3
Name: aha
Date: May 22, 2008 at 22:06:33 Pacific
Reply:

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


0

Response Number 4
Name: aha
Date: May 22, 2008 at 22:16:20 Pacific
Reply:

It seems like not for your question.
Sorry.

Phil

Computer Programmer


0

Sponsored Link
Ads by Google
Reply to Message Icon

Related Posts

See More







Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Database Forum Home


Sponsored links

Ads by Google


Results for: XML and Tree

MySQL5 and GUI creation software www.computing.net/answers/dbase/mysql5-and-gui-creation-software/184.html

get data from 1 table and ... www.computing.net/answers/dbase/get-data-from-1-table-and-/112.html

Taking out The, A, and An in sortin www.computing.net/answers/dbase/taking-out-the-a-and-an-in-sortin/57.html