XML细节点
编辑工具
FREE EDITOR :
EditiX XML Editor (NO SOAP / WSDL Support)
XML Copy Editor (NO SOAP / WSDL Support)
需要付费的:
Altova XMLSpy
oXygen XML Editor
在线XML Validation and Conversion Tools
XML Validator
www.stg.brown.edu/service/xmlvalid/
◆ DTD Validator
www.validome.org/grammar
◆ XML Schema Validator
www.w3.org/2001/03/webdata/xsv
◆ Convert XML to DTD / XML Schema;
Convert DTD to XML Schema
www.hitsw.com/xml_utilities/
确保XML数据的格式是正确的,必须通过验证
Rules for Writing XML
1.The first line of the XML document
<?xml version="1.0"?> is the XML declaration which notes which version of XML you are using.
2.
begins the data part of the document and is called the root element.
In an XML document, there can be only one root element.
也就是说一个XML文件必须有Root Element,它并不是XML的数据显示。
3. Closing tags are required
4. Elements must be properly nested
5. Case matters
6. Values must be enclosed in quotation marks
细节点
XML 空格的处理:
针对于attribute的Value(存在单双引号的问题)
If an attribute’s value contains double quotes, use single quotes to contain the value (and vice versa).
For example,
comments= 'She said, "The Colossus has fallen!"'.
Text
1. 存在Predefined Entities转义问题
To write the five predefined entities:
&: Type & to create an ampersand character(&).
<: Type < to create a less than sign (<).
>: Type > to create a greater than sign (>).
" : Type " to create a double quotationmark (").
' : Type ' to create a single quotation mark or apostrophe (').
Using Empty Elements
针对comments
1. No spaces are required between the double hyphens and the content of the comments itself. In other words <!--this is a comment--> is perfectly fine.
2. You may not use a double hyphen within a comment itself.
3. You may not nest comments within other comments.
Using Empty Elements
针对CDATA
CDATA: 对于XML而言,CDATA中的数据是不直接解析的。
1. CDATA stands for (unparsed) Character Data, meaning that the CDATA content will not be interpreted by the XML processor.
2. The special meaning that symbols have is ignored in the CDATA section. To display the less than and ampersand symbols, you would write < and &.
(对于<,&这两个符号在CDATA区中是不需要转义的)
3. If, for some reason, you want to write ]]> and you are not closing a CDATA section, the > must be written as >.
(对于大于号,即’>’,则必须要转义)
PHP与XML
XML在PHP中的应用大多分成: SOA 以及 配置文件两种
SOA: 使用XML来为其它的终端提供数据交换格式
PHP解析XML的方式有近三种
event-driven library based on the Expat Clibrary, one DOM-based library
Parsing XML with DOM
Parsing XML with SimpleXML(使用最频繁,操作简单,但是缺点是解析的XML必须要validate )
详见:
programming PHP 2nd
Apress.Pro.PHP.Programming.Aug.2011
Parsing XML with SimpleXML
Read XML
simplexml_load_file()
simplexml_load_string()
$xs = file_get_contents("test.xml");
$data = new SimpleXMLElement($xs);
对于使用SimpleXMLElement来解析,一定要对XML数据进行验证,进而再对数据进行加工
libxml_use_internal_errors(true);
$image_xml = simplexml_load_string($xml_content);
foreach( libxml_get_errors() as $err ) {
var_dump($err);
}
Write and Generate XML
$simplexml = new SimpleXMLElement('<?xml version="1.0"?><concerts />');
$concert1 = $simplexml->addChild('concert');
$concert1->addChild("title", "The Magic Flute");
$concert1->addChild("time", 1329636600);
echo $simplexml->asXML();
详见 PHP.Master:Write.Cutting-Edge.Code
No.Starch.Wicked.Cool.PHP.Feb.2008
浙公网安备 33010602011771号