[CareerCup] 17.10 Encode XML 编码XML
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了[CareerCup] 17.10 Encode XML 编码XML,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含3643字,纯文字阅读大概需要6分钟。
内容图文
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"; } elseif (tag == "person") { return "2"; } elseif (tag == "firstName") { return "3"; } elseif (tag == "lastName") { return "4"; } elseif (tag == "state") { return "5"; } return "--"; } } import java.util.ArrayList; publicclass 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"; } elseif (name == "person") { return "2"; } elseif (name == "firstName") { return "3"; } elseif (name == "lastName") { return "4"; } elseif (name == "state") { return "5"; } return "--"; } publicvoid insert(Attribute attribute) { attributes.add(attribute); } publicvoid insert(Element child) { children.add(child); } } publicclass j { publicstaticvoid encode(String v, StringBuffer sb) { v = v.replace("0", "\\0"); sb.append(v); sb.append(" "); } publicstaticvoid encodeEnd(StringBuffer sb) { sb.append("0"); sb.append(" "); } publicstaticvoid encode(Attribute attr, StringBuffer sb) { encode(attr.getTagCode(), sb); encode(attr.value, sb); } publicstaticvoid 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); } publicstatic String encodeToString(Element root) { StringBuffer sb = new StringBuffer(); encode(root, sb); return sb.toString(); } publicstaticvoid 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); } }
原文:http://www.cnblogs.com/grandyang/p/5438158.html
内容总结
以上是互联网集市为您收集整理的[CareerCup] 17.10 Encode XML 编码XML全部内容,希望文章能够帮你解决[CareerCup] 17.10 Encode XML 编码XML所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。