[CareerCup] 17.10 Encode XML 编码XML

简介:

17.10 Since XML is very verbose, you are given a way of encoding it where each tag gets mapped to a pre-defined integer value. The language/grammar is as follows:

Element --> Tag Attributes END Children END
Attribute --> Tag Value
END --> 0
Tag --> some predefined mapping to int
Value --> string value END
For example, the following XML might be 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 an XML element (passed in E Lament and Attribute objects).

这道题让我们给XML编码,那么我们可以用OOD的方法来实现,建立Attribute类和Element类,其中一个Element中可以包含多个Attribute,然后就是写encode函数,我们可以利用重载特性来写多个encode函数,需要注意的是,在encode到字符串中包含0的时候需要特殊处理,因为题目中说END都要换成0,为了不引起歧义,我们在字符串中出现0的地方前面都加一个\,变成\0,但是\0又表示空格,所以我们需要把所有的0变成\\0,可以用v = v.replace("0", "\\0"); 不得不说的是Java中对字符串的处理实在太强大了,集成了诸如replace, trim, split这样方便又常用的函数,而C++的STL却木有这些功能函数,还是Java叼啊。

public class Attribute {
    public String tag;
    public String value;
    
    public Attribute(String t, String v) {
        tag = t;
        value = v;
    }
    
    public String 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 "--";
    }
}

import java.util.ArrayList;

public class Element {
    public ArrayList<Attribute> attributes;
    public ArrayList<Element> children;
    public String name;
    public String value;
    
    public Element(String n) {
        name = n;
        attributes = new ArrayList<Attribute>();
        children = new ArrayList<Element>();
    }
    
    public Element(String n, String v) {
        name = n;
        value = v;
        attributes = new ArrayList<Attribute>();
        children = new ArrayList<Element>();
    }
    
    public String 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 void insert(Attribute attribute) {
        attributes.add(attribute);
    }
    
    public void insert(Element child) {
        children.add(child);
    }
}

public class j {
    public static void encode(String v, StringBuffer sb) {
        v = v.replace("0", "\\0");
        sb.append(v);
        sb.append(" ");
    }
    
    public static void encodeEnd(StringBuffer sb) {
        sb.append("0");
        sb.append(" ");
    }
    
    public static void encode(Attribute attr, StringBuffer sb) {
        encode(attr.getTagCode(), sb);
        encode(attr.value, sb);
    }
    
    public static void encode(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 static String encodeToString(Element root) {
        StringBuffer sb = new StringBuffer();
        encode(root, sb);
        return sb.toString();
    }
    
    public static void main(String args[]) {
        Element root = new Element("family");
        Attribute a1 = new Attribute("lastName", "0");
        Attribute a2 = new Attribute("state", "CA");
        root.insert(a1);
        root.insert(a2);
        
        Element child = new Element("person", "Some Message");
        Attribute a3 = new Attribute("firstName", "Gayle");
        child.insert(a3);
        
        root.insert(child);
        
        String s = encodeToString(root);
        System.out.println(s);
    }
}

本文转自博客园Grandyang的博客,原文链接:编码XML[CareerCup] 17.10 Encode XML ,如需转载请自行联系原博主。

相关文章
|
XML 数据格式 Windows
springmvc web.xml文件配置中文编码过滤器
springmvc web.xml文件配置中文编码过滤器
378 0
|
XML JSON 数据格式
netty系列之:netty中常用的xml编码解码器
在json之前,xml是最常用的数据传输格式,虽然xml的冗余数据有点多,但是xml的结构简单清晰,至今仍然运用在程序中的不同地方,对于netty来说自然也提供了对于xml数据的支持。 netty对xml的支持表现在两个方面,第一个方面是将编码过后的多个xml数据进行frame拆分,每个frame包含一个完整的xml。另一方面是将分割好的frame进行xml的语义解析。 进行frame拆分可以使用XmlFrameDecoder,进行xml文件内容的解析则可以使用XmlDecoder,接下来我们会详细讲解两个decoder实现和使用。
|
JSON Java 关系型数据库
常见Serialize技术探秘(ObjectXXStream、XML、JSON、JDBC byte编码、Protobuf)
目前业界有各种各样的网络输出传输时的序列化和反序列化方案,它们在技术上的实现的初衷和背景有较大的区别,因此在设计的架构也会有很大的区别,最终在落地后的:解析速度、对系统的影响、传输数据的大小、可维护性及可阅读性等方面有着较大的区别,本文分享一些我在一些常见序列化技术的分析和理解: 文章分成3个部分: 1、列举常见的序列化和反序列化方案(ObjectXXStream、XML、JSON) 2、M
2063 0
|
存储 XML 数据格式
|
XML 网络协议 数据格式
mina的编码和解码以及断包的处理,发送自定义协议,仿qq聊天,发送xml或json
最近一段时间以来,mina很火,和移动开发一样,异常的火爆。前面写了几篇移动开发的文章,都还不错,你们的鼓励就是我最大的动力。好了,废话少说。我们来看下tcp通讯吧。<br> tcp通讯对于java来说是很简单的。就是socket,也就是大家常说的套接字。大家不要把它看的很难。说白了tcp通讯其实就是数据流的读写。一条输入流,一条输出流。分别复杂发消息和接收消息。<br> 明白了这些,
2274 0
System.Xml.XmlException: 给定编码中的字符无效。 第 XX 行,位置 YY。
Invalid character in the given encoding. Line XX, position XX.解决方法 
2185 0

相关课程

更多