您的位置:首页 > 其它

string转为document 根据需要不同转法 动态表单解析设计表单的内容

2015-05-07 14:25 357 查看
对于实现动态表单设计的表单时写在textare中的,因此需要进行解析



获取里面的输入框,(此时前台js解析不了,页面可能过于复杂,前台无法解析)

/**
* String 转换为 Document 对象
*
* @param xml 字符串
* @return Document 对象
*/
public static Document string2Doc(String xml) {
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = null;
Document doc = null;
InputSource source = null;
StringReader reader = null;
try {
builder = factory.newDocumentBuilder();
reader = new StringReader(xml);
source = new InputSource(reader);//使用字符流创建新的输入源
doc = builder.parse(source);
return doc;
} catch (Exception e) {
return null;
} finally {
if(reader != null){
reader.close();
}
}
}


本人需要解析后的内容,供参考

Document doc = OperateXMLByDOM.string2Doc("<html><body>"+formModel.getModelStyle().replaceAll(" ", " ")+"</body></html>");
//解析text
NodeList textList = doc.getElementsByTagName("input");
2015/5/7
for(int i = 0;i<textList.getLength();i++){
Node n =textList.item(i);
String proName;
String proColumn = n.getAttributes().getNamedItem("name").getTextContent();
Node title = n.getAttributes().getNamedItem("title");
if(title == null){
proName = proColumn;
}else{
proName = title.getTextContent();
}

}

//解析textarea
NodeList areaList = doc.getElementsByTagName("textarea");
for(int i = 0;i<areaList.getLength();i++){
Node n =areaList.item(i);
String proName;
String proColumn = n.getAttributes().getNamedItem("id").getTextContent();
Node name = n.getAttributes().getNamedItem("title");
if(name == null){
proName = proColumn;
}else{
proName = name.getTextContent();
}

}

//解析select
NodeList selectList = doc.getElementsByTagName("select");
for(int i = 0;i<selectList.getLength();i++){
Node n =selectList.item(i);
String proName;
String proColumn = n.getAttributes().getNamedItem("id").getTextContent();
Node name = n.getAttributes().getNamedItem("title");
if(name == null){
proName = proColumn;
}else{
proName = name.getTextContent();
}

}


进行一系列操作之后,需要展示表单,此时用java解析后传到前台总会少一部分的内容,所有改到前台解析

<script type="text/javascript">

$(document).ready(function(){
//${nodes}是后台传来的数据
var nodes = eval("("+'${nodes}'+")");
var it = $("input,textarea,select");
for(var i = 0; i<it.length;i++){
var id = $("#"+it[i].id);
id.replaceWith(nodes[it[i].id]);
}
});

</script>


至于动态表单,本人是根据输入框id存入到数据库动态修改表结构,读取时也是动态读取的,供参考
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐