[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 ...
随机推荐
- Linux下常用命令
1.判断桌面环境是Gnome还是KDE #update-alternatives --display x-session-manager
- RDS MySQL 全文检索相关问题的处理
RDS MySQL 全文检索相关问题 1. RDS MySQL 对全文检索的支持 2. RDS MySQL 全文检索相关参数 3. RDS MySQL 全文检索中文支持 3.1 MyISAM 引擎表 ...
- SQL SERVER数据库的表中修改字段的数据类型后,不能保存
在数据库里面建了一个表,可是由于对SQL SERVER的建表功能不熟悉,不知道把主键设成什么是好,就先设置了个TEXT类型,可是后来朋友们告诉我说,TEXT类型容易让数据文件变得很大,还 是改成一 ...
- 关于css的全面学习笔记
1.text-align 属性规定元素中的文本的水平对齐方式.该属性通过指定行框与哪个点对齐,从而设置块级元素内文本的水平对齐方式.通过允许用户代理调整行内容中字母和字之间的间隔,可以支持值 just ...
- AngularJS学习之SQL
1.使用PHP从MySQL中读取数据: <div ng-app="myApp" ng-controller="customersCtrl" > &l ...
- POJ 2785 HASH
题目链接:http://poj.org/problem?id=2785 题意:给定n行数字,每行4个数分别是a,b,c,d,现在要求能有多少个(a,b,c,d)组合并且和为0 思路:n^2统计所有(a ...
- js:方法2. 字符串
String.charAt()/String.charCodeAt() string.charAt(n); n:The index of the character that should be re ...
- this对象
this对象 1.纯粹的函数调用 function test(){ this.x = 1; alert(this.x); } test();//1 2.函数作为某个对象的方法进行调用,这是this ...
- iOS10 UI教程视图的绘制与视图控制器和视图
iOS10 UI教程视图的绘制与视图控制器和视图 iOS10 UI视图的绘制 iOS10 UI教程视图的绘制与视图控制器和视图,在iOS中,有很多的绘图应用.这些应用大多是在UIView上进行绘制的. ...
- 我的c++学习(8)运算符重载和友元
运算符的重载,实际是一种特殊的函数重载,必须定义一个函数,并告诉C++编译器,当遇到该运算符时就调用此函数来行使运算符功能.这个函数叫做运算符重载函数(常为类的成员函数). 方法与解释 ◆ 1.定义运 ...