XML - Managing Data Exchange/NetBeans

From Wikibooks, open books for an open world
Jump to navigation Jump to search

NetBeans

Using an XML Editor: NetBeans : Single - Entity[edit | edit source]

Return to the Single Entity Chapter => A single entity

NetBeans IDE 5.0 instruction[edit | edit source]

  1. Launch NetBeans
  2. Make yourself familiar with the IDE by opening Help > Help Contents and reading the material in Getting Started
  3. Create a new project by File > New Project.. (Ctrl+Shift+N)
  4. Under "Choose a Project:" select under Categories: General
  5. Select under Projects: Java Application
  6. Press Next
  7. Name the project name appropriately (e.g., xml1)
  8. Check "Set as Main Project" and un-check "Create Main Class"
  9. Save to the appropriate location
  10. Press Finish
  11. Right click just created project (e.g., xml1) from Projects window
  12. Create a new file by New > File/Folder > XML > XML document
  13. Name the file (e.g., city)
  14. Press Next
  15. Select "Well-formed Document"
  16. Press Finish
  17. 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>
    
  18. 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>
    
  19. 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).
  20. 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 | edit source]

  1. Go to File -> New File -> XML -> XML Document.
  2. Save your file as ‘city’
  3. Select XML Schema-Constrained Document as the document type.
  4. Next, specify your schema location (Go to Browse, then select the schema just created) and root element name (tourGuide).
  5. Copy and paste the above code into the NetBeans editor and save.
  6. Check the XML document to make sure it is well-formed (ALT-F9).

To validate an XML document:

  1. 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:

Output Window - Validity check

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[edit | edit source]

The 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[edit | edit source]

Any 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:

  1. Go to File -> New Project

Use NetBeans to create the above XML schema:

  1. Go to File -> New File -> XML -> XML Schema.
  2. Save your file as ‘city’ (this gives the file an extension of .xsd).
  3. Copy and Paste the above schema into city.xsd and save

To check an XML document to see if it is well-formed:

  1. 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: Well Form XML Document

Summary: The XML schema shows the structure and elements of an XML document.

Netbeans Tips [edit | edit source]

Element 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[edit | edit source]

Use NetBeans to create the above XML stylesheet:

  1. Go to File -> New File -> XML -> XSL Stylesheet
  2. Save your file as ‘city’
  3. Copy and paste the above code into the NetBeans editor and save.
  4. 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 [edit | edit source]

The 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.