Labels

Friday, July 29, 2011

XML Terms

转载: http://cxjin.blog.51cto.com/81670/340300
  • 文档模型: 用于将 XML 数据当作树型对象进行解析和操纵的技术,也称作“拉”模型。请参阅作为示例的 DOM API 标准。
  • DOM: “文档对象模型”是 XML 文档的一种特殊树型结构编程模型,W3C 将它描述成一个标准。DOM 标准目前分成三个级别。DOM 1.0 是指 DOM 级别 1.0 一致性;DOM 级别 2 是最新的规范,W3C 正将它认可为“推荐”;DOM 级别 3 在本文创作时正在草拟中。
  • DTD: 文档类型定义。一种 XML 文件,定义了 XML 元素和那些元素的 XML 属性,而且指定了如何嵌套 XML 标记以及元素可以包含什么数据的规则。请参阅 Jane Fung 的 XML 和 DTD 简介以获取更多详细信息。
  • 事件模型: 用于通过使用回调或处理程序对 XML 数据进行解析的技术;也称作“推”模型。例如, 请参阅SAX API 标准。
  • 名称空间: 明确从不同的 DTD 或模式中标识出 XML 标记的方法,这样它们可以混合在同一个 XML 文档中。RDF 非常依赖于这个特性;XML 1.0 标记“xmlns”可以用于在 XML 文档中定义名称空间。
  • RDF: 资源描述框架,用于将 XML 属性数据和通常驻留在别处的信息相关联的一种压缩 XML 方言。您的驱动程序的许可证就类似于描述您的 RDF XML 文件。
  • SAX: Simple API for XML 是用于 XML 解析器实现的标准编程接口;SAX 使用面向事件的编程模型。SAX 是由 David Megginson 最早开发的事实上的标准,现在由 XML-dev 邮件列表维护。
  • SOAP: “简单对象访问协议”是类似于 XML-RPC( 请参阅XML-RPC)的网络协议。通过使用 SOAP,应用程序可以创建远程对象、调用该对象上的方法,以及检索结果。
  • 验证: 关于 DTD 和模式,验证结构良好的 XML 文档是否正确。
  • 结构良好: 一个 XML 文档,它的标记和数据符合 XML 1.0 语法。
  • W3C: 世界万维网联盟,它已经成为大多数 XML 相关技术的重要标准主体。W3C 将最终认可的规范称作“推荐”(而不是标准)。
  • XML 1.0: W3C 颂扬的第一个 XML 语法标准;建立了 XML 数据的基本规则,如所有标记都必须以斜杠( / )结束,如: ,或者后跟一个结束标记,如: close the tag on your way out
  • XML-RPC: XML 远程过程调用。XML-RPC 是用于跨网络调用方法和服务的标准 XML 方言;就象您认为的那样,XML-RPC 使用 XML 进行客户机和服务器之间的消息传递。
  • XML 模式: XML 模式是一种 W3C 推荐,它类似于 DTD,用于定义 XML 文档的结构,但具有更好的灵活性。XML 模式使用 XML 1.0 语法来指定模式,这与用于 DTD 的比较早期的 SGML 语法相反。
  • XQuery: 其部分功能类似于 XSLT,但设计得更有利于充当 XML 数据的查询语言 ― 类似于在关系数据库中使用 SQL。作为规范,它还不如 XSLT 成熟,XQuery 也许会成为今后十年里的 SQL。
  • XSLT: 可扩展样式语言转换,一种用于转换 XML 内容的 XML 方言。将 XSLT 文件应用于某些 XML 输入数据,以生成期望的 XML 输出数据。
These XML terms may come in handy as you read about the libraries discussed in this article:
  • Document model: Technique for parsing and manipulating XML data as a treelike object; this is also called a "pull" model. See the DOM API standard as an example.
  • DOM: The Document Object Model is a specific tree-structured programming model of an XML document described as a standard by the W3C. The DOM standard is currently divided into three levels. DOM 1.0 refers to DOM Level 1.0 conformance; DOM Level two is the most current spec that has been approved by the W3C as a Recommendation; DOM Level 3 is in draft at the time of this writing.
  • DTD: Document Type Definition. An XML file that defines XML elements and XML attributes for those elements and that specifies rules for how XML tags may be nested and what data an element may contain.
  • Event model: Technique for parsing XML data using callbacks or handlers; this is also called a "push" model. See the SAX API standard as an example.
  • Namespaces: Means of unambiguously identifying XML tags from different DTDs or schemas so they can be mixed in the same XML document. RDF is highly dependent upon this feature; the XML 1.0 token "xmlns" can be used to define a namespace within an XML document.
  • RDF: Resource Description Framework, a compact XML dialect for associating XML attribute data with information that usually resides elsewhere. Your driver's license would be analogous to an RDF XML file that describes you.
  • SAX: Simple API for XML is a standard programming interface for XML parser implementations; SAX uses an event-oriented programming model. SAX is a de facto standard first developed by David Megginson and now maintained by the XML-dev mailing list.
  • SOAP: Simple Object Access Protocol is a network protocol similar to XML-RPC (see XML-RPC). Using SOAP, an application can create a remote object, invoke methods on that object, and retrieve results.
  • Validation: Verifying that a well formed XML document is correct with respect to a DTD or schema.
  • Well formed: An XML document whose tags and data are consistent with XML 1.0 syntax.
  • W3C: The Worldwide Web Consortium, which has become the key standards body for most of the XML-related technologies. The W3C calls a finally approved specification a Recommendation (rather than a standard).
  • XML 1.0: The first standard for XML syntax blessed by the W3C; establishes basic rules for XML data, such as all tags must be closed with a slash (/) like this: <example/> or followed by a closing tag like this: <example>close the tag on your way out</example>.
  • XML-RPC: XML Remote Procedure Call. XML-RPC is a standard XML dialect for invoking methods and services across a network; as you can guess, XML-RPC uses XML for the messaging between client and server.
  • XML Schema: XML Schema, a W3C Recommendation, works much like a DTD to define the structure of an XML document but with more flexibility. XML Schema uses XML 1.0 syntax to specify the schema, in contrast to the older SGML syntax used for DTDs.
  • XQuery: Similar in some of its functionality to XSLT but designed more toward acting as a query language for XML data -- analogous to using SQL in a relational database. Less mature than XSLT as a specification, XQuery may become the SQL of the next decade.
  • XSLT: Extensible Style Language Transforms, an XML dialect for transforming XML content. You apply an XSLT file to some XML input data to produce the desired XML output data.

Thursday, July 28, 2011

Qt中使用DOM对XML进行的Read



我们知道,QT中对于XML文件的读写无非就两种方式,DOM和SAX,两种方法的具体介绍如下:
/**************************************************************/
用于读取和操作 XML 文件的标准是文档对象模型DOM。
DOM为 XML 文档的已解析版本定义了一组接口。解析器读入整个文档,然
后构建一个驻留内存的树结构,然后您的代码就可以使用 DOM 接口来操作这个树结构。您可以遍历树以了解原始文档包含了什么,您可以删除树的几个部分,还可以重新排列树和添加新的分支,等等。
遗憾的是,因为DOM 方法涉及读取整个文件并将该文件存储在一个树结构中,而这样可能是低效的、缓
慢的,并且很消耗资源:
DOM 构建整个文档驻留内存的树。如果文档很大,就会要求有极大的内存。
DOM 创建表示原始文档中每个东西的对象,包括元素、文本、属性和空格。如果您只需关注原始文档的
一小部分,那么创建那些永远不被使用的对象是极其浪费的。
DOM 解析器必须在您的代码取得控制权之前读取整个文档。对于非常大的文档,这会引起显著的延迟。
这些仅仅是由文档对象模型的设计引起的问题;撇开这些问题,DOM API 是解析 XML 文档非常有用的
方法。
一种替代技术就是SAX。相比于文档对象模型DOM,SAX 是读取和操作 XML 数据的更快速、更轻量的方法。SAX 允许您在读取文档时处理它,从而不必等待整个文档被存储之后才采取操作。它不涉及 DOM 所必需的开销和概念跳跃。
SAX API是一个基于事件的 API,适用于处理数据流,即随着数据的流动而依次处理数据。
SAX API 在其解析您的文档时发生一定事件的时候会通知您。在您对其响应时,您不作保存的数据将会
被抛弃。

/***************************************我是华丽的分割线*********************************/
我们可以看到,如果所要读取的XML文件不是很大,采用DOM读取方法还是很便捷的,由于我用的也是DOM树读取的方法,所以,本文所介绍的也主要是基于DOM的方法读取。
根据常用的操作,我简单的把对XML的操作分为以下几类:
1 首先对XML文件进行操作,打开文件。
这个其实就是对文件进行操作,可以把它直接定义为构造函数,在对对象进行初始化时完成。
TopoDataReader::TopoDataReader(const wstring &filePath):_filePath(filePath),_qDomDoc("mydocument"),_qFile(QString::fromStdWString(filePath))
{
if (!_qFile.open(QIODevice::ReadOnly))
{
return;
}

if (!_qDomDoc.setContent(&_qFile))
{
_qFile.close();
return;
}
}


2 读取XML文件中的节点内容
假设XML文件格式如下
(1)
<switchs>
<switch snmpip=211.87.235.136 newwork=front>
</switch>
</switchs>
(2)
<ip>211.87.235.136</ip>
对于第一种情况,采用如下方法:
QDomElement docElem = _qDomDoc.documentElement();
QDomNode nodeswitch=docElem.elementsByTagName("switch ");//红色的为标签名
QDomElement elemnodeswitch=nodeswitch.toElement();
string snmpip=qPrintable(elemnodeswitch.attribute("snmpip"));//network的也是如此方法获取
对于第二种情况,采用如下方法:
直接调用text() API就可以了
string ip=qPrintable(elementnodeip.text());
但是,假设文件中有多个同样的节点,如下
<a>
<b></b>
<b></b>
</a>
这样用elementsByTagName("b")返回的就是一个childNodes()而不是一个单独的node了。
我们可以使用at()方法具体定位。
另外,我们还可以使用这样一种方法获取节点的值,假设XML文件如下
- +<switch snmpIp="192.168.120.251" network="front">
<name>前端主交换机</name>
<description />
- <ipList>
<ip>192.168.120.251</ip>
</ipList>
<rwCommunity>public@120</rwCommunity>
<workMode>true</workMode>
<workStatus>true</workStatus>
<enableAlarm>true</enableAlarm>
<snmpCount>0</snmpCount>
<memoryUtilizationRatio>50.0</memoryUtilizationRatio>
<cpuUtilizationRatio>50.0</cpuUtilizationRatio>
<port>161</port>
<snmpStatus>true</snmpStatus>
<privateName>CZ-5_FA</privateName>
<switchIndex>topLeft</switchIndex>
</switch>
我们可以先获取switch节点,然后得到其childNodes(),于是就可以使用at()方法来获取下面的每个节点了(注:从0开始)
比如说,上面的那个ip节点就是nodeswitch.childNodes().at(3)。
这样做得好处就是不用担心重复的问题,只要你确定switch节点确定对了,底下的也就一定能确定了。
3 返回某个节点下子节点的个数
这个简单,也是直接调API
QDomElement docElem = _qDomDoc.documentElement();
QDomNode nodetagname=docElem.elementsByTagName(tagname).at(0);//假设有多个tagname此处选第一个
int num=nodetagname.childNodes().size();

Wednesday, July 27, 2011

Learning Perl Notes --2


Array and lists
A list is an ordered collection of scalars. An array is a variable that contains a list. They’re interchangeable. But, to be accurate, the list is the data, and the array is the variable.

  1. There’s no limit on array length which is automatically extended.
  2. To refer to an entire array, just use the @ before the name of the array (and no index brackets after it), for example @myArray.
  3. Access array element by index: $myArray[0] .
  4. the last element index is $#myArray.
  5. array copy: when an array is copied to another array, it’s still a list assignment. Ror example:
    @copy = @quarry; # copy a list from one array to another
  6. pop
    pop operator takes the last element off of an array and returns it:
    If the array is empty, pop will leave it alone, and it will return undef.
  7. push: adds an element (or a list of elements) to the end of an array
  8. The shift and unshift Operators : perform the corresponding actions on the “start” of the array

  1. The foreach Control Structure
    foreach $rock (qw/ bedrock slate lava /) {
    print "One rock is $rock.\n"; # Prints names of three rocks
    }
    The control variable ($rock in that example) takes on a new value from the list for each iteration. The first time through the loop, it’s "bedrock"; the third time, it’s "lava".
    Notes:
  • The control variable is not a copy of the list element—it actually is the list element. That is, if you modify the control variable inside the loop, you’ll be modifying the element itself .
  • If you omit the control variable from the beginning of the foreach loop, Perl uses its default variable, $_. For example: foreach (1..10) { # Uses $_ by default print "I can count to $_!\n"; } .

  1. reverse returns the reversed list; it doesn’t affect its arguments.
    If the return value isn’t assigned anywhere, it’s useless:
    reverse @fred; # doesn't change @fred
    @fred = reverse @fred; # puts the result back into the original array

Scalar and List Context
  1. As Perl is parsing your expressions, it always expects either a scalar value or a list value. What Perl expects is called the context of the expression
    @list = @people; # a list of three people
    $n = @people; # the number 3
  2. scalar: Forcing Scalar Context where Perl is expecting a list.
    print "I have ", @rocks, " rocks!\n"; # WRONG, prints all elements in @rocks array
    print "I have ", scalar @rocks, " rocks!\n"; # Correct, gives a number
  3. <STDIN> in List Context ,
    <STDIN> returns the next line of input in a scalar context.
    In list context, this operator returns all of the remaining lines up to the end of file. Each line is returned as a separate element of the list. If input is not from a file but the keyboard, then you’ll normally type a Control-D† to indicate to the system that there’s no more input;
    For example:
    @lines = <STDIN>; # read standard input in list context

Tuesday, July 26, 2011

kill a Windows process from the command line with taskkill

  1. Runs command line as Administrator
  2. Terminate gracefully, asking for confirmation if there are unsaved changes
taskkill /IM notepad.exe
  1. /F option terminate all matching processes without confirmation.
  2. To kill a single instance of a process, specify its process id (PID)
taskkill /PID 827
  1. Using filters, a variety of different patterns can be used to specify the processes to kill
For example, the following filter syntax will forcefully kill all processes owned by the user Quinn:
taskkill /F /FI "USERNAME eq Quinn"
The following table shows the available filters and their use.
Filter Name Valid Operators Valid Value(s)
----------- --------------- --------------
STATUS eq ne RUNNING | NOT RESPONDING
IMAGENAME eq ne Image name
PID eq ne gt lt ge le PID value
SESSION eq ne gt lt ge le Session number.
CPUTIME eq ne gt lt ge le CPU time in the format of hh:mm:ss.
MEMUSAGE eq ne gt lt ge le Memory usage in KB
USERNAME eq ne User name in [domain\]user format
MODULES eq ne DLL name
SERVICES eq ne Service name
WINDOWTITLE eq ne Window title
eq: equals
ne: not equal
gt: greater than
lt: less than
gt: greater than or equal
le: less than or equal

Monday, July 25, 2011

Learning Perl Notes --1

The following few posts are the learning notes of "Learning Perl".

Perl data and variable types
  1. All Numbers Have the Same Format Internally, double-precision floating-point value.
  2. Unlike C or C++, there’s nothing special about the NUL character in Perl because Perl uses length counting, not a null byte, to determine the end of the string.
  3. Strings
  • Single-Quoted String
Any character other than a single quote or a
backslash (including newline characters, if the string continues onto successive lines) stands for itself inside a string. Only when the backslash is followed by another backslash or a single quote does it have special meaning.
To get a backslash, put two backslashes in a row, and to get a single quote, put a backslash followed by a single quote.
\' and \\
  • double-Quoted String
the backslash takes on its full power to specify certain control characters, or even any character at all through octal and hex representations.

variable interpolated, some variable names within the string are replaced with their current values when the strings are used.

4. String operator
  • .   "hello" . "world" # same as "helloworld"
  • string repetition operator:  lowercase letter x
    "fred" x 3   # is "fredfredfred"
    5 × 4  # is really "5" × 4, which is "5555"   
Automatic Conversion Between Numbers and Strings
all depends upon the operator being used on the
scalar value. If an operator expects a number (like + does), Perl will see the value as a
number. If an operator expects a string (like . does), Perl will see the value as a string.
So you just use the proper operators, and Perl will performs all the conversions for you.



Binary Assignment
a raise to the power of operator is written as **=. So, $fred **= 3 means “raise the number in $fred to the third power, placing the result back in $fred.”

Comparison Operators
For comparing strings, Perl has an equivalent set of string comparison operators, which
look like funny little words: lt le eq ge gt ne.


Boolean Values
If the value is a number, 0 means false; all other numbers mean true. how about negative one??
• if the value is a string, the empty string ('') means false; all other strings mean true.
• if the value is another kind of scalar than a number or a string, convert it to a number or a string and try again.
There’s one trick hidden in those rules. Because the string '0' is the exact same scalar value as the number 0, Perl has to treat them both the same. That means that the string '0' is the only nonempty string that is false.

User Input
  • <STDIN> for user keyboard input

  • The chomp Operator
$text = "a line of text\n"; or from <STDIN>
chomp($text); # Gets rid of the newline character
Note: If a line ends with two or more newlines,* chomp removes only one. If there’s no newline,
it does nothing, and returns zero.

Undef and define function

The line-input operator <STDIN> is one operator that can return undef. Normally, it will return a line of text, but if there is no more input, such as at end-of-file, it returns undef.

To tell whether a value is undef and not the empty string, use the defined function, which returns false for undef, and true for everything else:
$madonna = <STDIN>;
if ( defined($madonna) ) {
print "The input was $madonna";
}

If you’d like to make your own undef values, you can use the obscurely named undef operator:
$madonna = undef; # As if it had never been touched















Friday, July 22, 2011

Install Mysql in Ubuntu

MySQL

MySQL is a fast, multi-threaded, multi-user, and robust SQL database server. It is intended for mission-critical, heavy-load production systems as well as for embedding into mass-deployed software.

Installation

To install MySQL, run the following command from a terminal prompt:
sudo apt-get install mysql-server
During the installation process you will be prompted to enter a password for the MySQL root user.
Once the installation is complete, the MySQL server should be started automatically. You can run the following command from a terminal prompt to check whether the MySQL server is running:

sudo netstat -tap | grep mysql

When you run this command, you should see the following line or something similar:
tcp        0      0 localhost.localdomain:mysql           *:* LISTEN -
If the server is not running correctly, you can type the following command to start it:

sudo /etc/init.d/mysql restart

Configuration

You can edit the /etc/mysql/my.cnf file to configure the basic settings -- log file, port number, etc. For example, to configure MySQL to listen for connections from network hosts, change the bind_address directive to the server's IP address:
bind-address            = 192.168.0.5
[Note]
Replace 192.168.0.5 with the appropriate address.
After making a change to /etc/mysql/my.cnf the mysql daemon will need to be restarted:
sudo /etc/init.d/mysql restart

Resources

How to move the window buttons

original link is 
http://www.howtogeek.com/howto/13535/move-window-buttons-back-to-the-right-in-ubuntu-10.04/ 

The window button locations are dictated by a configuration file. We’ll use the graphical program gconf-editor to change this configuration file.

Press Alt+F2 to bring up the Run Application dialog box, enter “gconf-editor” in the text field, and click on Run.

The key that we want to edit is in apps/metacity/general.
Click on the + button next to the “apps” folder, then beside “metacity” in the list of folders expanded for apps, and then click on the “general” folder.
sshot-4

The button layout can be changed by changing the “button_layout” key. Double-click button_layout to edit it.
Change the text in the Value text field to:
menu:maximize,minimize,close
sshot-5

Wednesday, July 20, 2011

Install insight from source on ubuntu

1. wget ftp://sourceware.org/pub/insight/releases/insight-6.8-1.tar.bz2
2. sudo apt-get install libx11-dev
3. sudo apt-get install libncurses5-dev
4. tar -xjvf insight-6.8-1.tar.bz2
5. cd insight-6.8-1/
6. ./configure –disable-werror
7. make
8. sudo make install