java attributelist_Java AttributeList.getName方法代码示例

import org.xml.sax.AttributeList; //导入方法依赖的package包/类

/**

* Initialisation routine called after handler creation

* with the element name and attributes. The attributes which

* this handler can deal with are: "default",

* "name", "id" and "basedir".

*

* @param tag Name of the element which caused this handler

* to be created. Should not be null.

* Ignored in this implementation.

* @param attrs Attributes of the element which caused this

* handler to be created. Must not be null.

*

* @exception SAXParseException if an unexpected attribute is

* encountered or if the "default" attribute

* is missing.

*/

public void init(String tag, AttributeList attrs) throws SAXParseException {

String def = null;

String name = null;

String id = null;

String baseDir = null;

for (int i = 0; i < attrs.getLength(); i++) {

String key = attrs.getName(i);

String value = attrs.getValue(i);

if ("default".equals(key)) {

def = value;

} else if ("name".equals(key)) {

name = value;

} else if ("id".equals(key)) {

id = value;

} else if ("basedir".equals(key)) {

baseDir = value;

} else {

throw new SAXParseException(

"Unexpected attribute \"" + attrs.getName(i)

+ "\"", helperImpl.locator);

}

}

if (def != null && !def.isEmpty()) {

helperImpl.project.setDefault(def);

} else {

throw new BuildException("The default attribute is required");

}

if (name != null) {

helperImpl.project.setName(name);

helperImpl.project.addReference(name, helperImpl.project);

}

if (id != null) {

helperImpl.project.addReference(id, helperImpl.project);

}

if (helperImpl.project.getProperty("basedir") != null) {

helperImpl.project.setBasedir(helperImpl.project.getProperty("basedir"));

} else {

if (baseDir == null) {

helperImpl.project.setBasedir(helperImpl.buildFileParent.getAbsolutePath());

} else {

// check whether the user has specified an absolute path

if ((new File(baseDir)).isAbsolute()) {

helperImpl.project.setBasedir(baseDir);

} else {

File resolvedBaseDir = FILE_UTILS.resolveFile(helperImpl.buildFileParent,

baseDir);

helperImpl.project.setBaseDir(resolvedBaseDir);

}

}

}

helperImpl.project.addTarget("", helperImpl.implicitTarget);

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
可以使用dom4j封装的工具类来实现Java解析XML字符串转Map的功能。具体步骤如下: 1.引入dom4j和jaxen的依赖包。 2.编写XmlUtils工具类,实现xmlParser方法,该方法接收两个参数,一个是xml字符串,一个是根节点名称,返回一个Map类型的结果。 ```java import java.util.HashMap; import java.util.Iterator; import java.util.List; import java.util.Map; import org.dom4j.Attribute; import org.dom4j.Document; import org.dom4j.DocumentException; import org.dom4j.Element; import org.dom4j.io.SAXReader; public class XmlUtils { public static Map<String, Object> xmlParser(String xml, String rootName) throws DocumentException { Map<String, Object> map = new HashMap<String, Object>(); SAXReader reader = new SAXReader(); Document document = reader.read(new ByteArrayInputStream(xml.getBytes())); Element root = document.getRootElement(); if (root.getName().equals(rootName)) { map.put(root.getName(), parseElement(root)); } return map; } private static Map<String, Object> parseElement(Element element) { Map<String, Object> map = new HashMap<String, Object>(); List<Attribute> attributes = element.attributes(); for (Attribute attribute : attributes) { map.put("->" + element.getName() + "=>" + attribute.getName(), attribute.getValue()); } List<Element> elements = element.elements(); if (elements.size() == 0) { map.put("->" + element.getName(), element.getTextTrim()); } else { for (Element ele : elements) { if (ele.elements().size() == 0) { map.put("->" + element.getName() + "->" + ele.getName(), ele.getTextTrim()); } else { map.put("->" + element.getName() + "->" + ele.getName(), parseElement(ele)); } } } return map; } } ``` 3.在需要解析XML字符串的地方,调用xmlParser方法即可。 ```java String xml = "<root><name>张三</name><age>18</age><address><province>广东</province><city>深圳</city></address></root>"; Map<String, Object> map = XmlUtils.xmlParser(xml, "root"); System.out.println(map); ``` 输出结果为: ```java {root={->name=张三, ->age=18, ->address={->province=广东, ->city=深圳}}} ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值