[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 ...
随机推荐
- 安装VS2010水晶报表插件
Visual Studio 2010默认不带水晶报表,需要安装一个水晶报表插件,首先下载此插件: http://downloads.businessobjects.com/akdlm/cr4vs201 ...
- 常见IE浏览器bug及其修复方案(双外边距、3像素偏移、绝对定位)
1. 双外边距浮动bug IE6和更低版本中存在双外边距浮动bug,顾名思义,这个Windows bug使任何浮动元素上的外边距加倍 bug重现: <!DOCTYPE html> < ...
- 【框架】网络请求+Gson解析--Retrofit 2
其实内部是封装了Okhttp和Gson解析 public class CourseFragmentAPI { public static void get(String userId, BaseCal ...
- caffe中添加local层
下载caffe-local,解压缩; 修改makefile.config:我是将cuudn注释掉,去掉cpu_only的注释; make all make test(其中local_test出错,将文 ...
- jquery的隐藏与显示
###显示与隐藏,通过用hide()和show()函数来实现 语法: $(selector).hide(speed,callback); $(selector).show(speed,callback ...
- 【noip2014T3】
上文有提到noip2014还有没A的嘛..就先把这个坑给填了 flappy bird好sad啊 还是先做解方程 八中的数据好强了,然而我最后凑了四个质数就A了,感谢shy! 作为联赛最后一题,学习它的 ...
- jQuery操作列表数据转成Json再输出为html dom树
jQuery 把列表数据转成Json再输出为如下 dom树 <div id="menu" class="lv1"> <ul class=&qu ...
- 贪心 Codeforces Round #288 (Div. 2) B. Anton and currency you all know
题目传送门 /* 题意:从前面找一个数字和末尾数字调换使得变成偶数且为最大 贪心:考虑两种情况:1. 有偶数且比末尾数字大(flag标记):2. 有偶数但都比末尾数字小(x位置标记) 仿照别人写的,再 ...
- UVa1486 Transportation(最小费用最大流)
题目大概说有n个城市,m条有向边连着它们,每条边都有两个属性一个是ai一个是ci,ai表示运送x单位货物时花费ai*x*x的价钱,ci表示边最多能运送的货物数量(<=5).问从城市1运送k单位货 ...
- 类型“System.Windows.Markup.IQueryAmbient”在未被引用的程序集中定义
错误 1 类型"System.Windows.Markup.IQueryAmbient"在未被引用的程序集中定义.必须添加对程序集"System.Xaml, ...