XML's Java resolution (1) (4)

zhaozj2021-02-17  45

The subject of shared label is before we start to explore more complex container structures, we have to figure another trouble of SAX. Although it is not common, data that appears in different locations in the XML document may share the same label name but belong to different Java objects. Suppose you have a Customer Node, there is a Cunstomer Representative node in the XML document. These nodes have the firstname and lastname tags. With this vague place, in the endelement event you can't determine which object in the cache is given. You also have to track the information in some StarTelement SAX events to clarify which object that is caused to collect data in the endelement SAX event.

If the XML document has not changed the DTD or DTD has changed, the mapping code has not changed, even if the XML document does not contain this structure, this issue is very dangerous. There is no DTD, your customers (the translation: refers to the user you compiled) legally gives you a label that you will map.

In fact, the only way for this problem is to keep tracking all open start tags. As a simple example, let's take a look at the XML document below:

Some Customer Name The customer's company name

Although the label name Name is not unique, the path to the label name is unique - either CustomerInformation-> Customer-> Name or CustomerInformation-> Customer-> Company-> Name. Always save the full path to ensure that the label name of the occasionally reuse will not let your code do not know what it is. This also shows that there is a problem that needs to be resolved when mapping recursive nested XML structures; we will discuss this problem in the next article.

Next, we see two examples of processing this situation. The first example is a violent IF solution. I want to set some flags when the STARTELEMENT SAX event contained. Then in the endelement event, I will run the IF statement with the flag to determine which object should be assigned to.

You will see our explanation of our XML sample documentation with our instructions.

3 0 3 5 < / width> 5 0 3 5 0 0 3 3 10 10 3 3 Next is all of our geometric Base class of graphics class:

Package Common; // The base class has a common attribute of geometric graphics. Public Class Shape {public int x = 0; public int y = 0; public int Height = 0; public int width = 0;}

Simple triangular class:

Package Common; Import Java.io. *; // Triangular Public Class Triangle Extends Shape {// Triangular Unique Attributes. . . Public string name = ""; public void print {Out.println ("Triange:" Name "x:" x "Y:" Y "Width:" width "Height: " height);}}

Next is a simple rectangular class:

Package Common; Import Java.io. *; // Rectangular Public Class Square Extends Shape {// Rectangle to be attribute. . . Public string name = ""; public void print {Out.println ("Square:" Name "x:" x "Y:" Y "Width:" width "Height: " height);}}

Package Common; IMPORT JAVA.IO. *; // Circular Class Circle Extends Shape {// Circular Unique Attribute Public String Name = ""; Public Void Print (PrintStream Out) {Out.println ("Circle:" Name "X:" x "Y:" y "width:" width "Height:" height);}} (unreasonable)

转载请注明原文地址:https://www.9cbs.com/read-30175.html

New Post(0)