XML 常见问题

1. Well-formed XML  Valid XML 的区别?
   
   
   
   
最大的区别:Well-formed XML完全遵守XML规范,Valid XML有自己的DTD文件。
   
   

 

A well formed document adheres to the following rules for writing an XML.

A root element is required. A root element is an element, which completely contains all the other elements.

Closing tags are required. <cust>abc</cust> or <cust/>

Elements must be properly nested.

XML is case sensitive. <CUSTOMER> and <Customer> elements are considered completely separate.

An attribute’s value must always be enclosed in either single or double quotes. Entity references must be declared in a DTD before being used except for the 5 built-in (&lt; for <, &gt; for >, &amp; for &, &quot; for “, &apos; for ‘.).

 

A valid XML document, it must conform to the rules of the corresponding DTD (Document Type Definition internal or external) or XSD (XML Schema Definition).

 

2. How do you write an attribute value with single quotes? How do you write an element value of “> 500.00”?

Use an internal entity reference like &lt; for <, &gt; for >, &amp; for &, &quot; for “, &apos; for ‘.

<customer name=”&quot;Mr. Smith&quot;” />

<cost> &gt; 500&apos;00</cost>

 

3. How do you write comments in an XML document?

<!-- This is an XML comment -->

 

4. What is a CDATA section in an XML?

CDATA section is displayed as a regular text.

<![CDATA[ <customername id=”123” > John </customername> ]]>

 

5. How will you embed an XML content within an XML document?

By using a CDATA section.

<message>

<from>LoansSystem</from>

<to>DocumentSystem</to>

<body>

<![CDATA[

<application>

<number>456</number>

<name>Peter</name>

<detail>blah blah</detail>

</application>

]]>

</body>

</message>

 

6. What is a namespace in an XML document?

Namespaces are used in XML to distinguish one similarly titled element from another. A namespace must have an absolutely unique and permanent name. In an XML, name space names are in the form of a URL. A default namespace for an element and all its children can be declared as follows:

<accounts xmlns=”http://www.bank1.com/ns/account”>

</accounts>

 

Individual elements can be labeled as follows:

<accounts xmlns=”http://www.bank1.com/ns/account” xmlns:bank2=”http://www.bank2.com/ns/account”>

<name>FlexiDirect</name> <!-- uses the default name space -->

<bank2:name>Loan</bank2:name> <!-- >

</accounts>

 

7. What is your favorite XML framework or a tool?

DOM, SAX, JDOM, Castor

 

8. Explain where your project needed XML documents?

It is hard to find a project, which does not use XML documents.

XML is used to communicate with disparate systems via messaging or Web Services.

XML based protocols and standards like SOAP, ebXML, WSDL etc are used in Web Services.

XML based deployment descriptors like web.xml, ejb-jar.xml, etc are used to configure the J2EE containers.

XML based configuration files are used by open-source frameworks like Hibernate, Spring, Struts, and Tapestry etc.

 

9. Which is better to store data as elements or as attributes in DTD or XSD?

The principle is data goes in elements and metadata goes in attributes. Elements are also useful when they contain special characters like “<”, “>”, etc which are harder to use in attributes. The most important reason to use element is its extensibility. It is far easier to create child elements to reflect complex content than to break an attribute into pieces. You can use attributes along with elements to refine your understanding of that element with extra information. Attributes are less verbose but using attributes instead of child elements with the view of optimizing document size is a short term strategy, which can have long term consequences.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值