<!--<br/ /><br/ />Code highlighting produced by Actipro CodeHighlighter (freeware)<br/ />http://www.CodeHighlighter.com/<br/ /><br/ />--> 1 <?php
2
3 /*
4 * This file is part of the symfony package.
5 * (c) 2004-2006 Fabien Potencier <fabien.potencier@symfony-project.com>
6 *
7 * For the full copyright and license information, please view the LICENSE
8 * file that was distributed with this source code.
9 */
10
11 /**
12 *
13 * @package symfony
14 * @subpackage util
15 * @author Fabien Potencier <fabien.potencier@symfony-project.com>
16 * @version SVN: $Id: sfFillInForm.class.php 2976 2006-12-08 19:11:36Z fabien $
17 */
18 class sfFillInForm
19 {
20 protected
21 $converters = array(),
22 $skipFields = array(),
23 $types = array('text', 'checkbox', 'radio', 'hidden', 'password');
24
25 public function addConverter($callable, $fields)
26 {
27 foreach ((array) $fields as $field)
28 {
29 $this->converters[$field][] = $callable;
30 }
31 }
32
33 public function setSkipFields($fields)
34 {
35 $this->skipFields = $fields;
36 }
37
38 public function setTypes($types)
39 {
40 $this->types = $types;
41 }
42
43 public function fillInHtml($html, $formName, $formId, $values)
44 {
45 $dom = new DomDocument('1.0', sfConfig::get('sf_charset', 'UTF-8'));
46 @$dom->loadHTML($html);
47
48 $dom = $this->fillInDom($dom, $formName, $formId, $values);
49
50 return $dom->saveHTML();
51 }
52
53 public function fillInXml($xml, $formName, $formId, $values)
54 {
55 $dom = new DomDocument('1.0', sfConfig::get('sf_charset', 'UTF-8'));
56 @$dom->loadXML($xml);
57
58 $dom = $this->fillInDom($dom, $formName, $formId, $values);
59
60 return $dom->saveXML();
61 }
62
63 public function fillInDom($dom, $formName, $formId, $values)
64 {
65 $xpath = new DomXPath($dom);
66
67 $query = 'descendant::input[@name and (not(@type)';
68 foreach ($this->types as $type)
69 {
70 $query .= ' or @type="'.$type.'"';
71 }
72 $query .= ')] | descendant::textarea[@name] | descendant::select[@name]';
73
74 // find our form
75 if ($formName)
76 {
77 $xpath_query = '//form[@name="'.$formName.'"]';
78 }
79 elseif ($formId)
80 {
81 $xpath_query = '//form[@id="'.$formId.'"]';
82 }
83 else
84 {
85 $xpath_query = '//form';
86 }
87
88 $form = $xpath->query($xpath_query)->item(0);
89 if (!$form)
90 {
91 if (!$formName && !$formId)
92 {
93 throw new sfException('No form found in this page');
94 }
95 else
96 {
97 throw new sfException(sprintf('The form "%s" cannot be found', $formName ? $formName : $formId));
98 }
99 }
100
101 foreach ($xpath->query($query, $form) as $element)
102 {
103 $name = (string) $element->getAttribute('name');
104 $value = (string) $element->getAttribute('value');
105 $type = (string) $element->getAttribute('type');
106
107 // skip fields
108 if (!$this->hasValue($values, $name) || in_array($name, $this->skipFields))
109 {
110 continue;
111 }
112
113 if ($element->nodeName == 'input')
114 {
115 if ($type == 'checkbox' || $type == 'radio')
116 {
117 // checkbox and radio
118 $element->removeAttribute('checked');
119 if ($this->hasValue($values, $name) && ($this->getValue($values, $name) == $value || !$element->hasAttribute('value')))
120 {
121 $element->setAttribute('checked', 'checked');
122 }
123 }
124 else
125 {
126 // text input
127 $element->removeAttribute('value');
128 if ($this->hasValue($values, $name))
129 {
130 $element->setAttribute('value', $this->escapeValue($this->getValue($values, $name), $name));
131 }
132 }
133 }
134 else if ($element->nodeName == 'textarea')
135 {
136 $el = $element->cloneNode(false);
137 $el->appendChild($dom->createTextNode($this->escapeValue($this->getValue($values, $name), $name)));
138 $element->parentNode->replaceChild($el, $element);
139 }
140 else if ($element->nodeName == 'select')
141 {
142 // select
143 $value = $this->getValue($values, $name);
144 $multiple = $element->hasAttribute('multiple');
145 foreach ($xpath->query('descendant::option', $element) as $option)
146 {
147 $option->removeAttribute('selected');
148 if ($multiple && is_array($value))
149 {
150 if (in_array($option->getAttribute('value'), $value))
151 {
152 $option->setAttribute('selected', 'selected');
153 }
154 }
155 else if ($value == $option->getAttribute('value'))
156 {
157 $option->setAttribute('selected', 'selected');
158 }
159 }
160 }
161 }
162
163 return $dom;
164 }
165
166 protected function hasValue($values, $name)
167 {
168 if (array_key_exists($name, $values))
169 {
170 return true;
171 }
172
173 return null !== sfToolkit::getArrayValueForPath($values, $name);
174 }
175
176 protected function getValue($values, $name)
177 {
178 if (array_key_exists($name, $values))
179 {
180 return $values[$name];
181 }
182
183 return sfToolkit::getArrayValueForPath($values, $name);
184 }
185
186 protected function escapeValue($value, $name)
187 {
188 if (function_exists('iconv') && strtolower(sfConfig::get('sf_charset')) != 'utf-8')
189 {
190 $new_value = iconv(sfConfig::get('sf_charset'), 'UTF-8', $value);
191 if (false !== $new_value)
192 {
193 $value = $new_value;
194 }
195 }
196
197 if (isset($this->converters[$name]))
198 {
199 foreach ($this->converters[$name] as $callable)
200 {
201 $value = call_user_func($callable, $value);
202 }
203 }
204
205 return $value;
206 }
207 }
208
相关推荐
dom+xpath读取xml并导入oracle.rar
dom4j和xpath的jar包,解析xml的最佳拍档.
您可能感兴趣的文章:c#通过xpath读取xml示例java使用xpath解析xml示例分享java使用xpath和dom4j解析xml深入XPath的详解以及Java示例代码分析解析XPath语法之在C#中使用XPath的示例详解使用HtmlAgilityPack XPath ...
dom4j 和dom4j 是一款比较好用的Xml解析工具jar,其中部分功能需要xpath的支持
dom4j和xpath必备jar包对xml文档进行解析的必须jar包!
资源来自pypi官网,解压后可用。 资源全名:py_dom_xpath_six-0.2.4-py3-none-any.whl
mysql和Xpath和Dom4J 的jar包
dom4j基础入门文档,与 W3C DOM API 相比,使用 dom4j 所包含的解析器的好处是 dom4j 拥有本地的 XPath 支持。
SelectDomXpathandCsspathSelect all Dom Xpath and Css path项目说明文档请见 ./app/README.md
xml解析笔记(dom,xpath,xquery等) 开发过程中做的一些笔记,各种格式XML文档的定义、语法、常用方法等,包括解析方法及代码实例。
List itemList = doc.selectNodes("/ACCESOS/item/SOCIO"); //以上是dom4j+xpath的读取xml的写法
PHP 文件在载入xml文件和创建DOMDocument 对象后,就可以开始建立DOMXPath 对象。建立形式如下: 复制代码 代码如下:$xpath = new DOMXPath($xmldoc); 建立 DOMXPath 对象后,就可以开始使用 DOMXPath::query()方法...
详细介绍dom4j_xpath,相信对想学dom4j_xpath的朋友有帮助
xml+dom4j+xpath资料
xml+Xpath基本应用,含有Dom4J及支持Xpath所需要的jar包,及xpath的入门教程,并附带一份源代码,提供对xml文件的基本读写及内容的增删改查的操作,方便学习xml,xpath及dom4j
Dom4j 解析XML 所使用的jar包,及添加XPath 支持所需要的 jar 包。具体使用可看我博客
dom4j帮助文档,以及xpath的参考文档,菜鸟必备工具。
无标题dom4j 和XPath jar包