XML - Managing Data Exchange/NetBeans
NetBeans
Using an XML Editor: NetBeans : Single - Entity
editNetBeans IDE 5.0 instruction
edit- Launch NetBeans
- Make yourself familiar with the IDE by opening Help > Help Contents and reading the material in Getting Started
- Create a new project by File > New Project.. (Ctrl+Shift+N)
- Under "Choose a Project:" select under Categories: General
- Select under Projects: Java Application
- Press Next
- Name the project name appropriately (e.g., xml1)
- Check "Set as Main Project" and un-check "Create Main Class"
- Save to the appropriate location
- Press Finish
- Right click just created project (e.g., xml1) from Projects window
- Create a new file by New > File/Folder > XML > XML document
- Name the file (e.g., city)
- Press Next
- Select "Well-formed Document"
- Press Finish
- You should see the following skeleton XML file:
<?xml version="1.0" encoding="UTF-8"?> <!-- Document : city.xml Created on : December 26, 2005, 11:18 AM Author : rtw Description: Purpose of the document follows. --> <root> </root>
- The goal is to create a simple markup file, rather than an XML file, so replace the section delimited by
<root>
and</root>
in the skeleton XML file with the lines in Table 1. After the change, the file should look like:<?xml version="1.0" encoding="UTF-8"?> <!-- Document : city.xml Created on : December 26, 2005, 11:18 AM Author : rtw Description: Purpose of the document follows. --> <city> <cityname>Athens</cityname> <state>GA</state> <description> Home of the University of Georgia</description> <population>100,000</population> <location>Located about 60 miles Northeast of Atlanta</location> <latitude>33 57' 39"N</latitude> <longitude>83 22' 42" W</longitude> </city>
- Check the markup file is well-formed by clicking on the single green downward point triangle on the XML tool bar above the document listing. It should pass the check. Alternatively, use Alt+F9 (Windows) or Ctrl+F9 (Mac).
- Delete the tag
</city>
and check the file again. This time you should get an error indicating that an end tag is missing. "XML document structures must start and end within the same entity." followed by the line number (where the error occurs) in brackets should appear in blue and underlined.
NetBeans is an open source IDE that includes an XML editor.
XML document:
edit- Go to File -> New File -> XML -> XML Document.
- Save your file as ‘city’
- Select XML Schema-Constrained Document as the document type.
- Next, specify your schema location (Go to Browse, then select the schema just created) and root element name (tourGuide).
- Copy and paste the above code into the NetBeans editor and save.
- Check the XML document to make sure it is well-formed (ALT-F9).
To validate an XML document:
- Right-click the document's node and choose Validate XML.
If the XML file, city.xml, conforms to the XML schema provided, city.xsd, your Output Window should look like the following:
Open this file, city.xml, in NetBeans.
Summary: Being a file which contains XML code and syntax, the XML document is an entity of neatly organized elements and subelements. |
Netbeans Tips
editThe prefix "xsi" for attribute "xsi:noNamespaceSchemaLocation" associated with an element type "Listing" is not bound.
-Caused because line "xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'" is missing from the top of the document.
cvc-elt.1: Cannot find the declaration of element 'Listing'.
-Caused due to lack of extension(.xsd) at the end of the schema name.
cvc-complex-type.2.4.a: Invalid content was found starting with element 'MuseumName'. One of '{"":Museum}' is expected. [5]
-Caused due to lack of naming at the beginning and end of the variable naming section.
The element type "MuseumName" must be terminated by the matching end-tag "</MuseumName>".
-Could be caused by a missing "/" in the end tag of one of the variables
XML Schema
editAny XML document, including XML schema and XML stylesheet documents, can be checked to make sure they are “well-formed”. An XML document is said to be well-formed if it adheres to the XML syntax rules, as outlined in the Chapter 1, such as there must be exactly one root element, every start tag must have an end tag. The well-formedness of an XML documents can be checked using an XML editor software, in this case, the NetBeans. If any errors are detected, they are listed along with the line number in the Output window. This location is not necessarily the line where the error occurred. By double-clicking on any error message, you can go to the line in the XML document where the error was detected.
Use NetBeans to create a new project:
- Go to File -> New Project
Use NetBeans to create the above XML schema:
- Go to File -> New File -> XML -> XML Schema.
- Save your file as ‘city’ (this gives the file an extension of .xsd).
- Copy and Paste the above schema into city.xsd and save
To check an XML document to see if it is well-formed:
- Right-click the document's node (under files, city.xsd) and choose Check XML schema or press Alt-F9.
If the XML schema, city.xsd, is well-formed, your Output Window should look like this:
Summary: The XML schema shows the structure and elements of an XML document. |
Netbeans Tips
editElement type "xsd:element" must be followed by either attribute specifications, ">" or "/>".
-Caused by lack of ">" or "/>" symbol at the end of either the specified line or the line there after.
The value of attribute "name" associated with an element type "xsd:complexType" must not contain the '<' character.
-Often caused by lack of closing quotation around the variable name on either the specified line or the line there after.
src-resolve.4.2: Error resolving component 'xsd:strin'...
-Caused by invalid "type", most likely misspelled. Can be for any "type", not just "string."
The element type "xsd:sequence" must be terminated by the matching end-tag "</xsd:sequence>".
-Caused because </xsd:sequence> is not found in order to close the statement. -Also caused because the closing statements are out of order as they at the beginning of the Schema.
XML Stylesheet
editUse NetBeans to create the above XML stylesheet:
- Go to File -> New File -> XML -> XSL Stylesheet
- Save your file as ‘city’
- Copy and paste the above code into the NetBeans editor and save.
- Check the stylesheet to make sure it is well-formed (ALT-F9).
Note: The city example files (city.xsd, city.xml and city.xsl) can be created using a text editor, and the results are displayed correctly in a compatible web browser such as Mozilla 1.6. You will need to store all of the files in the same directory and insert the following line after line 1 of city.xml: <?xml-stylesheet href="city.xsl" type="text/xsl"?>
This associates the XSL Stylesheet with the XML file containing the city data. Also, If you are using Netbeans, be sure to take off the xmlns line in the beginning which looks like xmlns='http://xml.netbeans.org/examples/targetNS', otherwise it will not display all data.
Netbeans Tips
editThe element type "xsl:text" must be terminated by the matching end-tag "</xsl:text>".
-Caused by either lack of end-tag or misspelling of the end- tag.
The element type "xsl:for-each" must be terminated by the matching end-tag "</xsl:for-each>".
-Caused due to either lack of end-tag for the specified tag or end-tags placed in the wrong order.