Java Web--xml

Source: Internet
Author: User
Tags cdata file url one more line processing instruction xpath java web

XML language (Extensible Markup Language): Describes a series of relational data, allowing custom tags, which are commonly used as software configuration files to describe the relationship between program modules.

    • XML syntax: Document declarations, elements, annotations (data area, special characters, processing instructions (processing instruction))
      • Document declaration: Declaring the type of document
        • Example: <?xml version= "1.0"?> <?xml Version = "1.0" encoding = "gb2312"?>
        • Garbled problem: The browser defaults to GB2312 format for parsing, and text documents saved in the UTF-8 format, there will be garbled.
      • Element: The label that appears in the XML file
        • can be nested, but not cross-nested
        • The label can be <a>...</a> with the main body, or it can be <a without the body/>
        • Must have only one root tag
        • Line breaks and spaces in labels are original files and cannot be formatted in a chaotic format
        • Label specification: Cannot start with a number and "_", cannot start with XML (XML or XML), cannot have spaces, and does not contain ":" in the middle.
      • Property:
        • Attribute values must be caused by "double-lead" or "single-lead"
        • You can use sub-labels, such as <input><name>...</name>
      • Note:<!----> declaration cannot have comments before, and cannot have nested annotations.
      • CDATA Zone: The content resolution engine does not parse the content and output it intact. < for programs >

<! [cdata[Content]]> Example: <! [Cdata[...<><> ...]] >

      • Escape character: Do not miss the semicolon

& &amp;  < &lt;  > &gt;  "-&quot; '-->&apos;

      • Processing instruction (PI): The processing instruction is used to direct the parsing engine to parse the XML document content. Example: <?xml-stylesheet type= "Text/css" href= "1.CSS"?> declaration statements are also processing instructions.
    • XML constraint: The author of a framework needs to write a constrained configuration file format
      • DTD constraint technology: Document type definition doc types definitions
<?XML version= "1.0"?>//xml File<!DOCTYPE Bookshelf SYSTEM "BOOK.DTD"><Bookshelf>    <Book>        <title>Java...</title>        <author>Zhang Xiaoxiang</author>        <Price>39.00 USD</Price>    </Book>    <Book>        ...    </Book></Bookshelf>

<! ELEMENT Bookshelf (book +>    //+ means    one or more <! ELEMENT Book (title, author, Price) >        <! ELEMENT title (#PCDATA) >        <! ELEMENT author (#PCDATA) >        <! ELEMENT Price (#PCDATA) >   
        • The DTD file should be encoded using UTF-8 or Unicode, and if it does not conform to the DTD rules, opening the XML under the browser can pass, so you should use Eclipse to verify.
        • There are two forms of DTD constraints, which can be written in XML or in a single file.
        • The XML file uses the DOCTYPE declaration statement to indicate the DTD file it follows.
          • <! DOCTYPE Document root node SYSTEM "DTD file URL" >
          • <! DOCTYPE Document root node Public "DTD name", "DTD file URL" > refers to a common file. For example:
<! DOCTYPE Web-app Public "-//sun microsystems.inc. DTD .... " "Http://java.sun.com/dtd/web_app_2_3.dtd">
      • DTD constraint Syntax details: (element definition, attribute definition, entity definition)
        • element definition: Using element in a DTD document Declares an XML element with a syntax of <! Element element type >
          • element type can be element content or type, such as <! ELEMENT Bookshelf (title, author, price) > and <! Element title (#PCDATA) > elements need to be enclosed in (). empty-> is used to define an empty element, any for any type, element type can be written directly. The composition of the contents of the
          • element: separated by commas, indicating that the order of occurrence is consistent; Delimited, which means that only one of them can appear.
          • syntax:
<! attlist element name        property Name 1        property value type        setting description ...      .   > such as: <! attlist Product        category        CDATA (equivalent to String)        #REQUIRED (properties that must be set)        color        CDATA (equivalent to string)        # Implied (optional properties)>
          • Setting Description: #REQUIRED为必须设置该属性, #IMPLIED可以设置也可以不设置, #FIXED取值固定一个值, but to provide this value and use the default value directly, you can modify it by default. Cases:
<! attlist page author        name        CDATA        #IMPLIED        Age        CDATA        #IMPLIED        Contact information  CDATA        #REQUIRED         website Title  CDATA        #FIXED        "page author"//default value can not write        personal hobbies  CDATA        "internet"//default can not write >
          • Attribute value type: CDATA---Normal text string enumerated enumeration (Chicken | pork | beef) ID--The unique identification of the label, which can only start with a letter, cannot appear space entity (entity)--Create an alias for a piece of content
            • Entity definition Syntax: <! Entity entity name "entities Content", which is referenced in XML by & entity name and entity content. For example
 <!  entity copyright "I am a programmer"  > //Use &copyright;   <!  entity% entity name "Entity content"  >  //with% entity name;--Entity content method Reference  <!  entity%tag_names "name | e-mails| Telephone | address " >  <!  element personal information (%tag_names;| birthday)  >  <!  element Customer information (%tag_names;| company name)  >  
    • XML programming (CRUD additions and deletions):
      • XML parsing methods: Dom and sax
        • DOM (Document Object model): Memory consumption is large, but adding and removing changes is easy. Features: tags, text, attributes are objects.
        • Sax (Simple API for XML): not an official standard, but a widespread use of sax. Features: Read a row to parse a row, memory consumption is small, not suitable for adding and removing changes.
      • XML parsing Development package: Jaxp (Sun), jdom,dom4j. Among them, dom4j best use.
        • JAXP: is a part of J2SE, consisting of javax.xml,org.w3c.dom,org.xml.sax and sub-packages. In the Javax.xml.parsers package, several factory classes are defined, and the programmer calls the factory class to get the DOM parser and the SAX parser for the XML document, thus parsing the XML.
          • Implementation: Documentbuilderfactory abstract class calls the Newinstance method to get the factory object, in the call object of the Parse method, returns a DOM document object, in the Document object, the implementation of XML documents deletion and modification.
          • Code:
Document doc = Builder.parse ("src/book.xml"= doc. Getelementbytagname ("title"= List.item (1= Node.gettextcontent ();

Additions and deletions can be referred to Api,document,node,element,transform and other completed.

    • Sax parsing: Parsing XML files in an event-handling manner, involving parsers and event handlers
      • The parser can be created using the JAXP API, creating a SAX parser to parse an XML document.
      • When the parser parses an XML document in sax, it calls the method of the time processor and passes the content to the time processor (parameter passing) as long as it resolves to the part of the XML document.
      • Through the parameters of the event handler, the parsed data can be obtained, thus determining the subsequent operation. (event handler written by programmer)
      • Flow: SAXParserFactory---SAXParser-Saxreader-->content Handle (incoming XML document)--event handler. The event handler can be written to view Api,content handle can take advantage of DefaultHandler inheritance and override the Start,character,end method.
      • JavaBean encapsulates XML document data:
        • First create a new JavaBean class, and then rewrite the DefaultHandler Start,character,end method, with the label as the condition of the judgment, deposited in the list. Note at the end of the label, you should clear the label of the record, otherwise, at the end of the label, skip to the Empty Line intermediate field and throw a null pointer exception.
    • dom4j parsing: (Hibernate is parsing the configuration file using dom4j):
      • Import the dom4j jar package in the JRE < note that both the jar package in its file and the support jar package are imported >. You can view the DOM4J documentation for a quick overview of DOM4J programming.
      • garbled problem: Save the data if there is garbled problem, you can initially determine the problem is IO stream, using OutputStream (File,charset) to choose the appropriate encoding method. Xmlwriter.write uses UTF-8 to write XML files, so if you need to write an XML file in GB2312 way, you need to define the format output. It is recommended to use a byte stream, because if a character stream is used, it is a character stream to check the Code table. If a byte stream is used, it is XmlWriter to check the Code table.
      • Add and remove changes:
        • <Create> Get the root node, and then look down to add the label of the parent tag, to join the label into the sequence, you need to get the parent tag's child tag collection {list=element ();}, and then call the List The Add method is inserted into the specified position.
        • <Delete> from the root node, point to the node that you want to delete, and then find the parent tag, and call the Remove method.
        • <Update> to find the destination node from the root node, call the SetText () method.
      • XPath: Quick positioning of XML data <xpath tutorial> document
        • syntax: Single slash: Write from the root element, double slash: Starting from the absolute path//*//bbb[ Properties]--insert document
        • code:
New= reader.read ( "...");d Oc.selectsinglenode ("//Author");    // get a single node doc.selectnodes ("...");    // Get all nodes
        • Match User name password:
 node node = Document.selectsinglenode ("//user[@username = '" +password+ "and @password = '" +password+ "']" ); if  (node = = null   " User name password error " else  {...}  
      • Schema constraint:
        • Characteristics:
          • The schema conforms to the XML syntax structure (XML is constrained by XML).
          • You can parse the schema using dom4j and so on (because the schema itself is an XML)
          • Schema exclusive namespace. schemas support more data types than DTDs, and are more constrained.
          • Entities cannot be defined and are more complex than DTDs. has become a standard.
        • Suffix. xsd, a schema document is often called a schema document, and the XML file that follows the document is called an instance document.
        • The XML schema must have a root node and must be the schema name.
        • After writing the schema document, you need to bind the elements declared in the document to the URI, which is to bind the elements declared in the XML Schema document to a namespace so that the parsing engine can find the parsing method later.
        • Namespaces : in the schema, each constrained schema document can be given a unique namespace, and in the case of XML writing tags, a namespace declaration (xmlns) can be used to indicate which schema document the label is currently written from.
<itcast: Bookshelf xmlns:itcast= "http://www.itcast.cn" >    <itcast: Book >....</itcast></itcast: Bookshelf >
          • To find the location of the specific file, you specify the SchemaLocation property. Cases:
<itcast: Bookshelf xmlns:itcast= "http://www.itcast.cn"xmlns:xsi= "Http://www.w3c.org/2001/XMLSchema-instance "xsi:schemalocation=" http://www.itcast.cn book.xsd ">
          • Use the default namespace: xmlns= "URI". For example:
< bookshelf xmlns= "www.itcast.cn" >    // Specifies a default namespace ...     ......     < book >....</book ></Bookshelf >
          • Using namespaces to introduce multiple XML Schema documents: Write one more line of xmlns, thanks for a schemalocation, you can use two constraint documents.
          • Do not use namespaces:< Bookshelf xml:xsi= .... xsi:nonamespaceschemalocation= "Xmlbook.xsd" >
      • Schema syntax: View documents

Java Web--xml

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.