JAVA-读取XML⽂件并将值赋给对象XML⽂件:div,XML
<?xml version="1.0" encoding="UTF-8"?>
<divstore>
<div id="1">
<divCode>
<div class="col-md-4 m-b-15">
<p>业务归属机构代码:</p>
<div class="p-relative">
<input type="text" class="form-control input-sm spinner-1 spinedit" id="comcode"
name="selectIdListDtoCode"
οnclick="windowOpen();"/>
</div>
</div>
</divCode>
</div>
<div id="2">
<divCode>
<div class="col-md-4 m-b-15">
<p> 短信发送号码:</p>
<div class="p-relative">
<input type="text" class="form-control input-sm spinner-1 spinedit" id="phoneNo"
name="selectIdListDto.phoneNo"/>
</div>
</div>
</divCode>
</div>
</divstore>
JAVA代码:读取XML⽂件并将值赋给对象
package action;
import java.io.IOException;
import java.util.ArrayList;java xml是什么
import java.util.List;
l.parsers.DocumentBuilder;
l.parsers.DocumentBuilderFactory;
l.parsers.ParserConfigurationException;
import org.w3c.dom.Document;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
l.sax.SAXException;
public class uploadAction {
public List<SelectCodeDto> readXml(String path){
DocumentBuilderFactory dbf = wInstance();
List<SelectCodeDto> list = new ArrayList<SelectCodeDto>();
try {
DocumentBuilder db = wDocumentBuilder();
/* 获取XML⽂件 */
/* 获取XML⽂件 */
Document document = db.parse(path);
NodeList divList = ElementsByTagName("div");
for (int i = 0; i < Length(); i++) {
/* 需赋值的对象 */
SelectCodeDto object = new SelectCodeDto();
Node div = divList.item(i);
/* 遍历第⼀层节点 */
NamedNodeMap attrs = Attributes();
for (int j = 0; j < Length(); j++) {
Node attr = attrs.item(j);
object.NodeValue());
}
/* 遍历第⼆层节点 */
NodeList childNodes = ChildNodes();
for (int k = 0; k < Length(); k++) {
if(childNodes.item(k).getNodeType() == Node.ELEMENT_NODE){
object.setDivCode(childNodes.item(k).getFirstChild().getNodeValue()); }
}
list.add(object);
}
} catch (ParserConfigurationException e) {
e.printStackTrace();
} catch (SAXException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return list;
}
public static void main(String[] args) {
uploadAction a = new uploadAction();
String[] aa = {"1","4","7"};
for(SelectCodeDto object : a.readXml("C:\\Users\\HZD\\Desktop\\l")){ //System.out.Id()+":==:"+DivCode());
}
}}
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论