17.10 Since XML is very verbose, you are given a-to-do encoding it where each tag gets mapped to a pre-defined integer VA Lue. The Language/grammar is as follows:
Element---Tag Attributes End Children End
Attribute to Tag Value
END-to 0
Tag--some predefined mapping to int
Value--String value END
For example, the following XML might is converted into the compressed string below (assuming a mapping of famiLy-L, Person->2, Frstname->3, LastName-4j State-5).
<family lastname= "McDowell" state= "CA" >
<person firstname= "Gayle" >some message</person>
</family>
becomes:
1 4 McDowell 5 CA 0 2 3 Gayle 0 Some Message 0 0
Write code to print the encoded version of a XML element (passed in E Lament and Attribute objects).
This problem lets us encode the XML, then we can use Ood method to implement, build attribute class and element class, one element can contain multiple attribute, then write encode function, We can use the overloaded features to write multiple encode functions, it should be noted that in the encode to the string contains 0 of the time need special handling, because the title is said end to 0, in order not to cause ambiguity, we appear in the string 0 in front of the place to add a \, become a, but \ 0 also means the space, so we need to turn all 0 into \\0, can be used V = v.replace ("0", "\\0"); I have to say that Java in the processing of strings is too powerful, integrated with such as replace, trim, split, such as convenient and commonly used functions, and C + + STL has these functions, or Java in the mouth ah.
Public classAttribute { PublicString tag; PublicString value; PublicAttribute (String T, string v) {tag=T; Value=v; } PublicString Gettagcode () {if(Tag = = "Family") { return"1"; } Else if(tag = "Person") { return"2"; } Else if(Tag = = "FirstName") { return"3"; } Else if(Tag = = "LastName") { return"4"; } Else if(Tag = = "State")) { return"5"; } return"--"; }}Importjava.util.ArrayList; Public classElement { PublicArraylist<attribute>attributes; PublicArraylist<element>children; PublicString name; PublicString value; PublicElement (String N) {name=N; Attributes=NewArraylist<attribute>(); Children=NewArraylist<element>(); } PublicElement (string n, String v) {name=N; Value=v; Attributes=NewArraylist<attribute>(); Children=NewArraylist<element>(); } PublicString Getnamecode () {if(name = = "Family")) { return"1"; } Else if(name = = "Person") { return"2"; } Else if(name = = "FirstName")) { return"3"; } Else if(name = = "LastName")) { return"4"; } Else if(name = = "State")) { return"5"; } return"--"; } Public voidInsert (Attribute Attribute) {attributes.add (Attribute); } Public voidInsert (Element child) {children.add (child); }} Public classJ { Public Static voidencode (String V, stringbuffer sb) {v= V.replace ("0", "\\0"); Sb.append (v); Sb.append (" "); } Public Static voidencodeend (StringBuffer sb) {sb.append ("0"); Sb.append (" "); } Public Static voidencode (Attribute attr, StringBuffer sb) {encode (Attr.gettagcode (), SB); Encode (Attr.value, SB); } Public Static voidencode (Element root, StringBuffer sb) {encode (Root.getnamecode (), SB); for(Attribute a:root.attributes) {encode (A, SB); } encodeend (SB); if(Root.value! =NULL&& Root.value! = "") {encode (root.value, SB); } Else { for(Element E:root.children) {encode (E, SB); }} encodeend (SB); } Public StaticString encodetostring (Element root) {StringBuffer sb=NewStringBuffer (); Encode (root, SB); returnsb.tostring (); } Public Static voidMain (String args[]) {Element root=NewElement ("Family"); Attribute A1=NewAttribute ("LastName", "0"); Attribute A2=NewAttribute ("state", "CA"); Root.insert (A1); Root.insert (A2); Element Child=NewElement ("Person", "Some Message"); Attribute A3=NewAttribute ("FirstName", "Gayle"); Child.insert (A3); Root.insert (child); String s=encodetostring (root); System.out.println (s); }}
Careercup all in one topic summary
[Careercup] 17.10 Encode XML encoded XML