有没有办法使用SAX解析器使用访问者模式?(Is there a way to use the Visitor pattern using a SAX Parser?)
我对此很好奇:如果我需要使用Sax解析器来提高效率(这是一个很大的文件)。 通常我使用这样的东西:
public class Example extends DefaultHandler { private Stack stack = new Stack (); public void startElement (String uri, String local, String qName, Attributes atts) throws SAXException { stack.push (qName); } public void endElement (String uri, String local, String qName) throws SAXException { if ("line".equals (qName)) System.out.println (); stack.pop (); } public void characters (char buf [], int offset, int length) throws SAXException { if (!"line".equals (stack.peek ())) return; System.out.write (new String (buf, offset, length)); } }
从这里采取的例子。
Sax已经是一个访问者模式的实现,但在我的情况下,我只需要获取每个元素的内容,并根据元素本身的性质对其进行处理。
我典型的XML文件是这样的:
<?xml version="1.0" encoding="utf-8"?> <labs xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> <auth> <uid> </uid> <gid> </gid> <key> </key> </auth> <campaign> <sms> <newsletter>206</newsletter> <message> <from>Da Definire</from> <subject>Da definire</subject> <body><![CDATA[Testo Da Definire]]></body> </message> <delivery method="manual"></delivery> <recipients> <db>276</db> <filter> <test>1538</test> </filter> <new_recipients> <csv_file>Corso2012_SMS.csv</csv_file> </new_recipients> </recipients> </sms> </campaign> </labs>
当我在csv_file节点中时,我需要获取文件名并从该文件上传用户,如果我在
filter/test
,则需要检查过滤器是否存在等等。 有没有办法使用SAX来应用访问者模式?I'm curious about this: if I need to use a Sax parser to boost up efficiency (it's a big file). Usually I use something like this:
public class Example extends DefaultHandler { private Stack stack = new Stack (); public void startElement (String uri, String local, String qName, Attributes atts) throws SAXException { stack.push (qName); } public void endElement (String uri, String local, String qName) throws SAXException { if ("line".equals (qName)) System.out.println (); stack.pop (); } public void characters (char buf [], int offset, int length) throws SAXException { if (!"line".equals (stack.peek ())) return; System.out.write (new String (buf, offset, length)); } }
example taken from here.
The Sax is already an implementation of a Visitor Pattern but in my case I just need to take the content of every element and do something with it according to the nature of the element itself.
My typical XML file is something like:
<?xml version="1.0" encoding="utf-8"?> <labs xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> <auth> <uid> </uid> <gid> </gid> <key> </key> </auth> <campaign> <sms> <newsletter>206</newsletter> <message> <from>Da Definire</from> <subject>Da definire</subject> <body><![CDATA[Testo Da Definire]]></body> </message> <delivery method="manual"></delivery> <recipients> <db>276</db> <filter> <test>1538</test> </filter> <new_recipients> <csv_file>Corso2012_SMS.csv</csv_file> </new_recipients> </recipients> </sms> </campaign> </labs>
When I'm in the csv_file node I need to take the filename and upload users from that file, if I'm in the
filter/test
I need to check if the filter exists and so on. Is there a way to apply the Visitor Pattern with SAX?
原文:https://stackoverflow.com/questions/10850989
最满意答案
当您更新帖子时,您宁愿向给定资源发送
POST
(或更好的PATCH
/PUT
- http://www.w3.org/Protocols/rfc2616/rfc2616-sec9.html )请求。也就是说,您将在网址中包含已编辑的行键,并将您的方法更改为以下内容:
// route Route::post('/postedit/{id}', array('as' => 'postedit', 'uses' => 'AdminController@postEdit')) ->before('auth'); // controller public function postEdit($id) { // if no posts with $id found, throws exception - catch it and eg. show 404 $post = Post::findOrFail($id); $rules = [ 'title' => 'required', 'article' => 'required', 'slug' => 'required|unique:posts,slug,'.$id, // to ignore this row in unique check 'category' => 'required' ]; // validate $post->fill($input)->save(); // fill() in order to use mass-assignement check // alternatively you can just update: // $post->update($input); // but then make sure $input has only elements corresponding to the table columns
另外,请阅读有关路由分组的信息 ,因此您无需单独向这些路由添加
before('auth')
。When you update a post, you'd rather send a
POST
(or betterPATCH
/PUT
- http://www.w3.org/Protocols/rfc2616/rfc2616-sec9.html) request to given resource.That said, you would include edited row key in the url, and change your method to something like this:
// route Route::post('/postedit/{id}', array('as' => 'postedit', 'uses' => 'AdminController@postEdit')) ->before('auth'); // controller public function postEdit($id) { // if no posts with $id found, throws exception - catch it and eg. show 404 $post = Post::findOrFail($id); $rules = [ 'title' => 'required', 'article' => 'required', 'slug' => 'required|unique:posts,slug,'.$id, // to ignore this row in unique check 'category' => 'required' ]; // validate $post->fill($input)->save(); // fill() in order to use mass-assignement check // alternatively you can just update: // $post->update($input); // but then make sure $input has only elements corresponding to the table columns
Additionally, read about route grouping, so you don't need to add
before('auth')
to those routes separately.
相关问答
更多-
具有唯一索引和主键的列会产生唯一约束违例(Column with Unique Index and Primary Key gives Unique Constraint Violation)[2022-08-17]
请执行一个简单的声明: select * from admin.message_list where id = 1; 如果它不返回行,则应检查约束PK_ID是否引用ID列,如果是,请要求Oracle支持人员修复错误。 Please execute a simple statement: select * from admin.message_list where id = 1; If it wouldn't return rows, you should check if constraint PK_I ... -
唯一键约束违规时的主键标识值增量(Primary Key Identity Value Increments On Unique Key Constraint Violation)[2024-03-24]
是的,这很正常。 想象一下这里发生的事务,这是在SQL Server上运行的潜在操作顺序。 使用ID 1,2,3,4,5。 客户A开始交易。 客户端A执行插入但不提交(ID 6)。 客户B开始交易。 客户端B执行插入但不提交。 (ID 7)。 客户A回滚。 客户B提交。 由于(不一定存在)此行为的可能性,您会看到在插入失败时跳过ID 6。 Yes, this is normal. Imagine transactions going on here and this being a potential or ... -
编辑时唯一的密钥违规(Unique key violation upon edit)[2022-04-17]
当您更新帖子时,您宁愿向给定资源发送POST (或更好的PATCH / PUT - http://www.w3.org/Protocols/rfc2616/rfc2616-sec9.html )请求。 也就是说,您将在网址中包含已编辑的行键,并将您的方法更改为以下内容: // route Route::post('/postedit/{id}', array('as' => 'postedit', 'uses' => 'AdminController@postEdit')) ->before('au ... -
PostgreSQL中的serial类型非常简单:它本质上是一个integer列,其默认值来自序列 。 但是序列不知道你正在对表执行什么操作,因此如果在不使用或更新serial情况下为serial指定值,事情就会变得混乱。 例如: create table t ( id serial not null primary key ); insert into t (id) values (1); insert into t (id) values (DEFAULT); 将产生唯一性违规,因为1明确用于id ...
-
您可以使用而不是插入触发器。 在触发器内部,执行Insert不存在的表格CommReceipt.CR_Key = inserted.CR_Key。 Create trigger T_CommReceiptInsteadOfInsert on CommReceipt Instead of Insert As Begin --Insert duplicate records into another table Insert Into CommReceipt_Duplicates(CR_Key, ...) S ...
-
这应该工作得很好。 什么是实际的表定义? 你的整理是什么? -- Working Example CREATE TABLE dbo.[Table]( Column1 VARCHAR(50) , Column2 VARCHAR(50) , Column3 INT , Column4 VARCHAR(25) ); ALTER TABLE [dbo].[Table] ADD CONSTRAINT [UC_Table_Column1_Column2_Column3_Colum ...
-
找到一个不错的枢轴点,并围绕该枢轴移动数据。 例如,如果你所有的ID都是正数,那么一个好的数值就是0。 当你通常会这样做 UPDATE Test SET Id = Id+1; 改为做这个序列 UPDATE Test SET Id = -Id; UPDATE Test SET Id = -Id +1; 有时候,你可以找到一个类似的支点,但公式只是一点点。 Find a nice pivot point, and move the data around that pivot. For example, i ...
-
Jan,希望这会对你有所帮助: https : //www.sqlite.org/lang_conflict.html Jan, hope this will help you: https://www.sqlite.org/lang_conflict.html
-
您是否尝试过http://grails.org/doc/latest/ref/Constraints/unique.html上描述的最后一个示例? 例如:postStatus(空白:false,可空:false,唯一:['postType','basicProfile']) Have you tried the last example described at http://grails.org/doc/latest/ref/Constraints/unique.html ? e.g: postStatu ...
-
在两台服务器上运行以下命令: select * from sysmessages sm where sm.error = 2627 您应该看到消息模板不同。 您正在使用哪个确切版本的SQL Server 2008,因为它在2008 R2中绝对可用: 违反%ls约束'%。* ls'。 无法在对象'%。* ls'中插入重复键。 重复键值为%ls。 通过模板回顾SQL Server 2005数据库: 违反%ls约束'%。* ls'。 无法在对象'%。* ls'中插入重复键。 从查看您的@@version可以清楚 ...