[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);
}
}
[CareerCup] 17.10 Encode XML 编码XML的更多相关文章
- Python 入门基础17 --加密、表格、xml模块
今日内容: 1.hashlib模块:加密 2.hmac模块:加密 3.configparser模块:操作配置文件 4.subprocess模块:操作shell命令 5.xlrd模块:excel 6.x ...
- thinkphp xml编码函数
/** * XML编码 * @param mixed $data 数据 * @param string $root 根节点名 * @param string $item 数字索引的子节点名 * @pa ...
- 雷林鹏分享:XML 编码
XML 编码 XML 文档可以包含非 ASCII 字符,比如挪威语 æ ø å,或者法语 ê è é. 为了避免错误,需要规定 XML 编码,或者将 XML 文件存为 Unicode. XML 编码错 ...
- XML 参考:XML基础 XML 简介
XML 参考:XML基础 -- XML简介和用途 转:http://www.cnblogs.com/Dlonghow/archive/2009/01/22/1379799.html XML 参考:XM ...
- C#操作Xml:linq to xml操作XML
LINQ to XML提供了更方便的读写xml方式.前几篇文章的评论中总有朋友提,你为啥不用linq to xml?现在到时候了,linq to xml出场了. .Net中的System.Xml.Li ...
- hadoop三个配置文件的参数含义说明core-site.xml,hdfs-site.xml,mapred-site.xml
配置hadoop,主要是配置core-site.xml,hdfs-site.xml,mapred-site.xml三个配置文件,默认下来,这些配置文件都是空的,所以很难知道这些配置文件有哪些配置可以生 ...
- XML概念定义以及如何定义xml文件编写约束条件java解析xml DTD XML Schema JAXP java xml解析 dom4j 解析 xpath dom sax
本文主要涉及:xml概念描述,xml的约束文件,dtd,xsd文件的定义使用,如何在xml中引用xsd文件,如何使用java解析xml,解析xml方式dom sax,dom4j解析xml文件 XML来 ...
- linq to xml操作XML(转)
转自:http://www.cnblogs.com/yukaizhao/archive/2011/07/21/linq-to-xml.html LINQ to XML提供了更方便的读写xml方式.前几 ...
- Spring基础——配置文件pom.xml,web.xml,ApplicationContext.xml
Spring配置文件——复制粘贴即用 为了以后兼容SSM框架,直接创建Maven Project,包结构如下图. pom.xml <project xmlns="http://mave ...
随机推荐
- 思想&观点&人生
思想: 思想的直接表现往往是对事物的观点,观点越多并且越接近本质,表示思想越丰富和深刻 观点不是事实 观点是基于事实之上的一种系统性的判断和理解框架,事实是观点的基础 观点不一定正确 观点组成: 事实 ...
- Vue入门笔记#数据绑定语法
#数据绑定语法# #文本: 数据绑定的基础表型形式,使用“Mustache”语法(双大括号)(特意查了一下Mustache同“moustache”释义为:髭:上唇的胡子,小胡子,最起码我觉得挺形象的) ...
- oc中定时器的基本使用
// 时间间隔 调用的对象 调用的方法 用户信息 是否循环 [NSTimer scheduledTimerWithTimeInterval: target:self selector:@select ...
- JSON详解以及可以把javabean转换成json串的json-lib应用
JSON 1. json是什么 它是js提供的一种数据交换格式! 2. json的语法 {}:是对象! 属性名必须使用双引号括起来!单引不行!!! 属性值:null,数值,字符串,数组:使用[]括起来 ...
- 浏览器 - Firefox开发者附加组件
Firefox开发者版本下载地址: https://www.mozilla.org/zh-CN/firefox/channel/desktop/#developer 教程: https://devel ...
- SPOJ REPEATS 后缀数组
题目链接:http://www.spoj.com/problems/REPEATS/en/ 题意:首先定义了一个字符串的重复度.即一个字符串由一个子串重复k次构成.那么最大的k即是该字符串的重复度.现 ...
- zjoi2016 day1【bzoj4455】【bzoj4456】
首先做了T2的旅行者,看到bz上面过的人数比较多.. 考试的时候完全没有想太多.一闪而过了分块思想,然后就没有然后了.. 大视野上面有题解,竟然是一个初中生写的..? 正解其实是“分治”,每次选择中轴 ...
- 安装配置hive中遇到的问题
1. mysql中添加用户名时总出现如下问题:ERROR 1290 (HY000): The MySQL server is running with the --skip-grant-tables ...
- json日期格式问题的办法
//json日期转换 格式(2015-01-01) <input class="easyui-datebox" name="sbdj_txtShebaoka_Lin ...
- Money类型转化为String去除小数点后0解决方法
Money类型转化为String去除小数点后0从数据库提取Money类型后,字符串如:1212.0000 如何使其成为1212 注:去掉了小数点 如果是:1212.0100 使 ...