[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 ...
随机推荐
- poj 3264 【线段树】
此题为入门级线段树 题意:给定Q(1<=Q<=200000)个数A1A2…AQ,多次求任一区间Ai-Aj中最大数和最小数的差 #include<algorithm> #incl ...
- sql 数字转人民币大写函数(两种方法)
,)) returns @rmb table( 亿 ) ,仟万 ) ,佰万 ) ,拾万 ) ,万 ) ,仟 ) ,佰 ) ,拾 ) ,元 ) ,角 ) ,分 )) as begin insert in ...
- Git的安装与使用
1,下载git https://code.google.com/p/msysgit/downloads/list 2,安装git ,我们选择命令行形式,这样无论在window下还是在linux下 都可 ...
- AchartEngine 的学习
第一步:我使用的事AchartEngine 1.1.0 的包.大家要先下在这个包,放到项目中,创建一个lib文件夹.然后倒金项目中去.然后再AndroidManifest.xml 中需要注册一下代码是 ...
- java 线程演示
package unit8; public class Mainthread { public static void main(String[] args) { Thread t = new Thr ...
- Liferay 6.2 改造系列之六:修改系统初始化信息
将初始化过程修改为:中文语言 在/portal-master/portal-impl/src/system.properties文件中,有如下配置: # # Set the default local ...
- 阿里云 OSS+CDN
https://promotion.aliyun.com/ntms/ossedu2.html https://www.aliyun.com/act/aliyun/ossdoc.html 对象存储(Ob ...
- 6754 Keyboard of a Mobile Telephone
/*实践再次说明ch=getchar()的速度非常慢*/ /*大水题,不解释*/ #include<stdio.h> #include<string.h> int main() ...
- [转] FastMM、FastCode、FastMove的使用
http://blog.csdn.net/akof1314/article/details/6524767 FastMM是一个替换Embarcadero Delphi Win32应用程序的快速内存管理 ...
- pycharm设置主题/默认格式/字体
1.步骤为:File-->Settings-->Appearance & Behavior-->Appearance-->Theme中就可以选择喜欢的主题 2.一般将文 ...