解析xml时怎么能取到 node里面的属性值
1个回答
推荐于2018-04-08 · 知道合伙人数码行家
huanglenzhi
知道合伙人数码行家
向TA提问 私信TA
知道合伙人数码行家
采纳数:117538
获赞数:517193
长期从事计算机组装,维护,网络组建及管理。对计算机硬件、操作系统安装、典型网络设备具有详细认知。
向TA提问 私信TA
关注
展开全部
一个节点一个值格式的XML解析生成办法
/**
* @name ParseXml2DtoByDom4j
* @title 解析XML并将其节点元素压入DTO
* @param String[XML字符串]
* @param boolean[根元素标志]
* @return DTO
* @description
* @author
*/
public static final OutputDTO ParseXml2DtoByDom4j(String strXml, boolean hasRoot){
OutputDTO outDto = new BaseOutputDTO();
String strTitle = " <?xml version=\"1.0\" encoding=\"UTF-8\"?>";
Document document = null;
try {
document = hasRoot ? DocumentHelper.parseText(strTitle + strXml)
: DocumentHelper.parseText(strTitle + " <root>" + strXml + " </root>");
} catch (DocumentException e) {
final String MSG = errorMsg + strXml ;
log.error(MSG, e);
}
Element root = document.getRootElement();
for ( Iterator i = root.elementIterator(); i.hasNext(); ) {
Element leaf = (Element) i.next();
outDto.put(leaf.getName().toLowerCase(), leaf.getData());
}
return outDto;
}
/**
* @name ParseDto2XmlByDom4j
* @title 将DTO中的Key-Value键值映射为标准的XML Document
* @param DTO
* @param boolean 是否带头信息
* @return String
* @description 说明:YHCIP已经提供了Dto.toXml()方法,但只能是简单转换.如果需要高级功能,则需在此方法上借助Dom4J扩展即可
* @author
*/
public static final String ParseDto2XmlByDom4j(DTO dto, boolean hasHead){
Document document = DocumentHelper.createDocument();
document.addElement("root");
Element root = document.getRootElement();
Iterator keyIterator = dto.keySet().iterator();
while(keyIterator.hasNext()){
String key = (String)keyIterator.next();
String value = dto.getAsString(key);
Element leaf = root.addElement(key);
leaf.setText(value);
}
String outXmlWithHead = document.asXML();
String outXmlWithoutHead = outXmlWithHead.substring(38);
return hasHead ? outXmlWithHead : outXmlWithoutHead;
}
3. 节点属性格式的XML解析和生成办法
/**
* @name ParseXml2DtoByDom4j
* @title 解析XML并将其节点元素压入DTO
* @param String[XML字符串]
* @param xPath[节点路径]例如:"//paralist/row" 则表示根节点paralist下的row节点的xPath路径
* @return OutputDTO
* @description 此方法支持将一个XML节点任意多个属性以 <K,V>存储结构动态的压入DTO中
*/
public static final DTO ParseXml2DtoByDom4j(String pStrXml, String pXPath){
DTO outDto = new BaseDTO();
String strTitle = " <?xml version=\"1.0\" encoding=\"UTF-8\"?>";
Document document = null;
try {
document = DocumentHelper.parseText(strTitle + pStrXml);
} catch (DocumentException e) {
log.error("将XML格式的字符串转换为XML DOM对象时发生错误.\n");
e.printStackTrace();
}
Element root = document.getRootElement();
root.attributeIterator();
Element elNode = (Element)document.selectSingleNode(pXPath);
for ( Iterator it = elNode.attributeIterator(); it.hasNext(); ) {
Attribute attribute = (Attribute) it.next();
outDto.put(attribute.getName(), attribute.getData());
}
return outDto;
}
/**
* @name ParseList2XmlByDom4j
* @title 将List转换为XML格式数据
* @param pList 传入的List数据
* @param pRootNodeName 根节点名称
* @param pPropNameValue 根节点name属性值
* @return String
* @description List里的对象类型为Map型
*/
public static final String ParseList2XmlByDom4j(List pList, String pRootNodeName, String pPropNameValue){
Document document = DocumentHelper.createDocument();
Element elRoot = document.addElement(pRootNodeName);
elRoot.addAttribute("name", pPropNameValue);
for(int i = 0; i < pList.size(); i++){
Map map = (Map)pList.get(i);
Element elRow = elRoot.addElement("row");
Iterator it = map.entrySet().iterator();
while(it.hasNext()){
Map.Entry entry = (Map.Entry)it.next();
elRow.addAttribute((String)entry.getKey(), String.valueOf(entry.getValue()));
}
}
String outXml = document.asXML();
return outXml.substring(39, outXml.length());
}
/**
* @name ParseXml2DtoByDom4j
* @title 解析XML并将其节点元素压入DTO
* @param String[XML字符串]
* @param boolean[根元素标志]
* @return DTO
* @description
* @author
*/
public static final OutputDTO ParseXml2DtoByDom4j(String strXml, boolean hasRoot){
OutputDTO outDto = new BaseOutputDTO();
String strTitle = " <?xml version=\"1.0\" encoding=\"UTF-8\"?>";
Document document = null;
try {
document = hasRoot ? DocumentHelper.parseText(strTitle + strXml)
: DocumentHelper.parseText(strTitle + " <root>" + strXml + " </root>");
} catch (DocumentException e) {
final String MSG = errorMsg + strXml ;
log.error(MSG, e);
}
Element root = document.getRootElement();
for ( Iterator i = root.elementIterator(); i.hasNext(); ) {
Element leaf = (Element) i.next();
outDto.put(leaf.getName().toLowerCase(), leaf.getData());
}
return outDto;
}
/**
* @name ParseDto2XmlByDom4j
* @title 将DTO中的Key-Value键值映射为标准的XML Document
* @param DTO
* @param boolean 是否带头信息
* @return String
* @description 说明:YHCIP已经提供了Dto.toXml()方法,但只能是简单转换.如果需要高级功能,则需在此方法上借助Dom4J扩展即可
* @author
*/
public static final String ParseDto2XmlByDom4j(DTO dto, boolean hasHead){
Document document = DocumentHelper.createDocument();
document.addElement("root");
Element root = document.getRootElement();
Iterator keyIterator = dto.keySet().iterator();
while(keyIterator.hasNext()){
String key = (String)keyIterator.next();
String value = dto.getAsString(key);
Element leaf = root.addElement(key);
leaf.setText(value);
}
String outXmlWithHead = document.asXML();
String outXmlWithoutHead = outXmlWithHead.substring(38);
return hasHead ? outXmlWithHead : outXmlWithoutHead;
}
3. 节点属性格式的XML解析和生成办法
/**
* @name ParseXml2DtoByDom4j
* @title 解析XML并将其节点元素压入DTO
* @param String[XML字符串]
* @param xPath[节点路径]例如:"//paralist/row" 则表示根节点paralist下的row节点的xPath路径
* @return OutputDTO
* @description 此方法支持将一个XML节点任意多个属性以 <K,V>存储结构动态的压入DTO中
*/
public static final DTO ParseXml2DtoByDom4j(String pStrXml, String pXPath){
DTO outDto = new BaseDTO();
String strTitle = " <?xml version=\"1.0\" encoding=\"UTF-8\"?>";
Document document = null;
try {
document = DocumentHelper.parseText(strTitle + pStrXml);
} catch (DocumentException e) {
log.error("将XML格式的字符串转换为XML DOM对象时发生错误.\n");
e.printStackTrace();
}
Element root = document.getRootElement();
root.attributeIterator();
Element elNode = (Element)document.selectSingleNode(pXPath);
for ( Iterator it = elNode.attributeIterator(); it.hasNext(); ) {
Attribute attribute = (Attribute) it.next();
outDto.put(attribute.getName(), attribute.getData());
}
return outDto;
}
/**
* @name ParseList2XmlByDom4j
* @title 将List转换为XML格式数据
* @param pList 传入的List数据
* @param pRootNodeName 根节点名称
* @param pPropNameValue 根节点name属性值
* @return String
* @description List里的对象类型为Map型
*/
public static final String ParseList2XmlByDom4j(List pList, String pRootNodeName, String pPropNameValue){
Document document = DocumentHelper.createDocument();
Element elRoot = document.addElement(pRootNodeName);
elRoot.addAttribute("name", pPropNameValue);
for(int i = 0; i < pList.size(); i++){
Map map = (Map)pList.get(i);
Element elRow = elRoot.addElement("row");
Iterator it = map.entrySet().iterator();
while(it.hasNext()){
Map.Entry entry = (Map.Entry)it.next();
elRow.addAttribute((String)entry.getKey(), String.valueOf(entry.getValue()));
}
}
String outXml = document.asXML();
return outXml.substring(39, outXml.length());
}
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询