Read this carefully:采用JAVA处理XML非常容易
1,你需要一个JDK
2,你需要一些 free class libraries,如XML4J,XERCES,LOTUSXSL
3,你需要一个text editor
4,你需要一些待处理的DATA预备条件
熟悉Java尤其是I/O, classes, objects, polymorphism等概念.
知道XML要well-formedness(格式准确), validity(有法为证), namespaces(名字空间)等。I will briefly review proper terminology 
一个简单的例子:
--------------------------------------------------------------------------------
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<?xml-stylesheet type="text/css" href="song.css"?>
<!DOCTYPE SONG SYSTEM "song.dtd">
<SONG xmlns="http://metalab.unc.edu/xml/namespace/song"
      xmlns:xlink="http://www.w3.org/1999/xlink">
  <TITLE>Hot Cop</TITLE>
  <PHOTO 
    xlink:type="simple" xlink:show="onLoad" xlink:href="hotcop.jpg"
    ALT="Victor Willis in Cop Outfit" WIDTH="100" HEIGHT="200"/>
  <COMPOSER>Jacques Morali</COMPOSER>
  <COMPOSER>Henri Belolo</COMPOSER>
  <COMPOSER>Victor Willis</COMPOSER>
  <PRODUCER>Jacques Morali</PRODUCER>
  <!-- The publisher is actually Polygram but I needed 
       an example of a general entity reference. -->
  <PUBLISHER xlink:type="simple" xlink:href="http://www.amrecords.com/">
    A &amp; M Records
  </PUBLISHER>
  <LENGTH>6:20</LENGTH>
  <YEAR>1978</YEAR>
  <ARTIST>Village People</ARTIST>
</SONG>
<!-- You can tell what album I was 
     listening to when I wrote this example -->
Markup and Character Data Markup includes: 
1,Tags
2,Entity References
3,Comments
4,Processing Instructions
5,Document Type Declarations
6,XML Declaration
7,CDATA Section Delimiters
8,Character data includes everything else例子:(标记和Character Data例子)
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<?xml-stylesheet type="text/css" href="song.css"?>
<!DOCTYPE SONG SYSTEM "song.dtd">
<SONG xmlns="http://metalab.unc.edu/xml/namespace/song"
      xmlns:xlink="http://www.w3.org/1999/xlink">
  <TITLE>Hot Cop</TITLE>
  <PHOTO 
    xlink:type="simple" xlink:show="onLoad" xlink:href="hotcop.jpg"
    ALT="Victor Willis in Cop Outfit" WIDTH="100" HEIGHT="200"/>
  <COMPOSER>Jacques Morali</COMPOSER>
  <COMPOSER>Henri Belolo</COMPOSER>
  <COMPOSER>Victor Willis</COMPOSER>
  <PRODUCER>Jacques Morali</PRODUCER>
  <!-- The publisher is actually Polygram but I needed 
       an example of a general entity reference. -->
  <PUBLISHER xlink:type="simple" xlink:href="http://www.amrecords.com/">
    A &amp; M Records
  </PUBLISHER>
  <LENGTH>6:20</LENGTH>
  <YEAR>1978</YEAR>
  <ARTIST>Village People</ARTIST>
</SONG>
<!-- You can tell what album I was 
     listening to when I wrote this example -->Entities
An XML document is made up of one or more physical storage units called entities 
Entity references : 
    Parsed internal general entity references like &amp; 
    Parsed external general entity references
    Unparsed external general entity references
    External parameter entity references
    Internal parameter entity references
    Reading an XML document is not the same thing as reading an XML file
    The file contains entity references.
    The file document contains the entities' replacement text.
    When you use a parser to read a document you'll get the text including characters like <. You will not see the entity references. 
Parsed Character Data
    Character data left after entity references are replaced with their text
    假设element如下:
    <PUBLISHER>A &amp; M Records</PUBLISHER> 
    那么parsed character data是:A & M Records 
CDATA 部分
    Used to include large blocks of text with lots of normally illegal literal characters like < and &, typically XML or HTML. <p>You can use a default <code>xmlns</code>
attribute to avoid having to add the svg prefix to all
your elements:</p>
<![CDATA][
  <svg xmlns="http://www.w3.org/Graphics/SVG/SVG-19991203.dtd" 
       width="12cm" height="10cm">
    <ellipse rx="110" ry="130" />
    <rect x="4cm" y="1cm" width="3cm" height="6cm" />
  </svg>
]]>    CDATA is for human authors, not for programs! Comments
<!-- Before posting this page, I need to double check the number of pelicans in Lousiana in 1970 --> 
Comments are for humans, not programs. 
Processing Instructions
    Divided into a target and data for the target:
    1,The target must be an XML name、
    2,The data can have an effectively arbitrary format
例:
<?robots index="yes" follow="no"?>
<?xml-stylesheet href="pelicans.css" type="text/css"?>
<?php 
  mysql_connect("database.unc.edu", "clerk", "password"); 
  $result = mysql("CYNW", "SELECT LastName, FirstName FROM Employees 
    ORDER BY LastName, FirstName"); 
  $i = 0;
  while ($i < mysql_numrows ($result)) {
     $fields = mysql_fetch_row($result);
     echo "<person>$fields[1] $fields[0] </person>\r\n";
     $i++;
  }
  mysql_close();
?>These are for programs(为程序服务!)The XML Declaration(XML声明)
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
Looks like a processing instruction but isn't. version attribute requiredalways has the value 1.0
encoding attribute UTF-88859_1etc.
standalone attribute yesnoDocument Type Declaration
<!DOCTYPE SONG SYSTEM "song.dtd"> 
Document Type Definition--------------------------------------------------------------------------------<!ELEMENT SONG (TITLE, COMPOSER+, PRODUCER*, 
PUBLISHER*, YEAR?, LENGTH?, ARTIST+)><!ELEMENT TITLE (#PCDATA)><!ELEMENT COMPOSER (#PCDATA)>
<!ELEMENT PRODUCER (#PCDATA)>
<!ELEMENT PUBLISHER (#PCDATA)>
<!ELEMENT LENGTH (#PCDATA)>
<!-- This should be a four digit year like "1999",
     not a two-digit year like "99" -->
<!ELEMENT YEAR (#PCDATA)><!ELEMENT ARTIST (#PCDATA)>
XML Names
Used for element, attribute, and entity namesCan contain any alphabetic, ideographic, or numeric Unicode characterCan contain hyphen, underscore, or periodCan also contain colons but these are reserved for namespacesCan begin with any alphabetic or ideographic character or the underscore but not digits or other punctuation s
XML Namespaces
Raison d'etre: To distinguish between elements and attributes from different vocabularies with different meanings.To group all related elements and attributes together so that a parser can easily recognize them.
Each element is given a prefixEach prefix (as well as the empty prefix) is associated with a URIElements with the same URI are in the same namespaceURIs are purely formal. They do not necessarily point to a page. 
--------------------------------------------------------------------------------
Namespace Syntax
Elements and attributes that are in namespaces have names that contain exactly one colon. They look like this: rdf:description
xlink:type
xsl:template
Everything before the colon is called the prefix Everything after the colon is called the local part or local name.The complete name including the colon is called the qualified name or raw name. 
--------------------------------------------------------------------------------
Namespace URIs
Each prefix in a qualified name is associated with a URI. For example, all elements in XSLT 1.0 style sheets are associated with the http://www.w3.org/1999/XSL/Transform URI.The customary prefix xsl is a shorthand for the longer URI http://www.w3.org/1999/XSL/Transform. You can't use the URI in the element name directly. 
Binding Prefixes to Namespace URIs
Prefixes are bound to namespace URIs by attaching an xmlns:prefix attribute to the prefixed element or one of its ancestors. <svg:svg xmlns:svg="http://www.w3.org/Graphics/SVG/SVG-19991203.dtd" 
width="12cm" height="10cm">
  <svg:ellipse rx="110" ry="130" />
  <svg:rect x="4cm" y="1cm" width="3cm" height="6cm" />
</svg:svg>
Bindings have scope within the element where they're declared. An SVG processor can recognize all three of these elements as SVG elements because they all have prefixes bound to the particular URI defined by the SVG specification. The Default Namespace
Indicate that an unprefixed element and all its unprefixed descendant elements belong to a particular namespace by attaching an xmlns attribute with no prefix: <DATASCHEMA xmlns="http://www.w3.org/2000/P3Pv1">
  <DATA name="vehicle.make" type="text" short="Make" 
        category="preference" size="31"/>
  <DATA name="vehicle.model" type="text" short="Model" 
        category="preference" size="31"/>
  <DATA name="vehicle.year" type="number" short="Year" 
        category="preference" size="4"/>
  <DATA name="vehicle.license.state." type="postal." short="State" 
        category="preference" size="2"/>
  <DATA name="vehicle.license.number" type="text" 
        short="License Plate Number" category="preference" size="12"/>
</DATASCHEMA>
Both the DATASCHEMA and DATA elements are in the http://www.w3.org/2000/P3Pv1 namespace. Default namespaces apply only to elements, not to attributes. Thus in the above example the name, type, short, category, and size attributes are not in any namespace. Unprefixed attributes are never in any namespace. You can change the default namespace within a particular element by adding an xmlns attribute to the element. 
How Parsers Handle Namespaces
Namespaces were added to XML 1.0 after the fact, but care was taken to ensure backwards compatibility. An XML 1.0 parser that does not know about namespaces will most likely not have any troubles reading a document that uses namespaces. A namespace aware parser also checks to see that all prefixes are mapped to URIs. Otherwise it behaves almost exactly like a non-namespace aware parser. Other software that sits on top of the raw XML parser, an XSLT engine for example, may treat elements differently depending on what namespace they belong to. However, the XML parser itself mostly doesn't care as long as all well-formedness and namespace constraints are met.A possible exception occurs in the unlikely event that elements with different prefixes belong to the same namespace or elements with the same prefix belong to different namespacesMany parsers have the option of whether to report namespace violations so that you can turn namespace processing on or off as you see fit.Canonical XML
A W3C standard for determining when two documents are the same after: Entity references are resolvedDocument is converted to UnicodeUnicode combining forms are combinedComments are strippedWhite space is normalizedDefault attribute values are added
If at all possible, your programs should depend only on the canonical form of the document 
Canonical form of hotcop.xml: 
<?xml-stylesheet type="text/css" href="song.css"?><SONG>&#10; <TITLE>Hot Cop</TITLE>&#10; <COMPOSER>Jacques Morali</COMPOSER>&#10; <COMPOSER>Henri Belolo</COMPOSER>&#10; <COMPOSER>Victor Willis</COMPOSER>&#10; <PRODUCER>Jacques Morali</PRODUCER>&#10; <PUBLISHER>A &amp; M Records</PUBLISHER>&#10; <LENGTH>6:20</LENGTH>&#10; <YEAR>1978</YEAR>&#10; <ARTIST>Village People</ARTIST>&#10;</SONG> Trees
An XML document is a tree.
It has a root.
It has nodes.
It is amenable to recursive processing.
Not all applications agree on what the root is.
Not all applications agree on what is and isn't a node.我将在下次给出一个简单的JAVA APP。--------------------------------------------------------------------------------
 
 

解决方案 »

  1.   

    使用java的jaxp包来读,它的指南的例子就够啦!
    http://java.sun.com/xml/tutorial_intro.html
      

  2.   

    re needle:
    里面的例题看了,不合我用啊,我要的比那复杂多了!!!唉!
    老天爷知道,我一共才学了几天JAVA,老板就找了那么个活!!
      

  3.   

    还有,现在对于简单的XML用DOM来显示,我已经没问题了
    现在的麻烦是,我现在最不清楚的就是要怎么把它们分类存取起来,
    然后根据不同的情况,用其中的某一部分配置!…………
      

  4.   

    happyloy, 可能你会需要到data binding, 已经有这个JSR了, 到IBM的developerWorks去看看吧, 另外还可以参考一下enhydra里的配置那部分, 你会喜欢的, hohoho~~~~~
      

  5.   

    学一学啦。
    http://www.cn.ibm.com/developerWorks/education/xml/xmljava/tutorial/xmljava-menu.html
      

  6.   

    我有个详细介绍xml的doc,想要的话我寄给你
      

  7.   

    DATA BINDING??看过一部分,想过,不过好像那个API还在开发啊,
    要是能用DOM、SAX来解决就省得麻烦了!
      

  8.   

    RE:backlove
    寄给我吧!!谢谢了
    [email protected]或是
    [email protected]