用dom4j 取xml节点属性问题,跪求高手解答

king_spf 2010-10-13 09:13:15
xml结构如下

<?xml version="1.0" encoding="UTF-8"?>
<SOAP-ENV:Envelope
xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema" id="attr">
<SOAP-ENV:Body>
<m:editBroadbandAccountRequest
xmlns:m="http://uac.bossuac.com.cn/">
<systemId>{SYSTEMID}</systemId>
<BroadbandAccount_2>
<userId>{USERID}</userId>
<userPassword>{USERPASSWORD}</userPassword>
<plcRate>{PLC_RATE}</plcRate>
<plcBurst>{PLC_BURST}</plcBurst>
<mtrRate>{MTR_RATE}</mtrRate>
<mtrBurst>{MTR_BURST}</mtrBurst>
<framedIpAddress>{FRAMEDIPADDRESS}</framedIpAddress>
<flowNo>{FLOWNO}</flowNo>
<userGroup>{USERGROUP}</userGroup>
<sendTime>{SENDTIME}</sendTime>
<userType>{USERTYPE}</userType>
<remainTime>{REMAINTIME}</remainTime>
<maxOnLineTime>{MAXONLINETIME}</maxOnLineTime>
<idleTimeout>{IDLETIMEOUT}</idleTimeout>
<web_Url>{WEB_URL}</web_Url>
<filter>{FILTER}</filter>
<bindMode NUM="N">{BINDMODE}</bindMode>
<vLan>{VLAN}</vLan>
<port>{PORT}</port>
<slotNo>{SLOTNO}</slotNo>
<xpi>{XPI}</xpi>
<xci>{XCI}</xci>
<macAddress>{MACADDRESS}</macAddress>
<zone>{ZONE}</zone>
<portLimit>{PORTLIMIT}</portLimit>
<profile_name>{PROFILE_NAME}</profile_name>
<validTime>{VALIDTIME}</validTime>
<frame_Pool>{FRAME_POOL}</frame_Pool>
<userName>{USERNAME}</userName>
<userTel>{USERTEL}</userTel>
<iDCardCode>{IDCARDCODE}</iDCardCode>
<userAddress>{USERADDRESS}</userAddress>
<emailAddress>{EMAILADDRESS}</emailAddress>
<prePaid>{PREPAID}</prePaid>
<measure>{MEASURE}</measure>
<useGroup>{USERGROUP}</useGroup>
<bindflag>{BINDFLAG}</bindflag>
<extString1>{PORT}</extString1>
<extString2>{PORTLIMIT}</extString2>
<extString3>{PREPAID}</extString3>
<extString4>{PROFILE_NAME}</extString4>
<extString5>{REMAINTIME}</extString5>
<extString6>{SENDTIME}</extString6>
<extString7>{SLOTNO}</extString7>
<extString8>{USERGROUP}</extString8>
<extString9>{USERADDRESS}</extString9>
<extString10>{USERGROUP}</extString10>
</BroadbandAccount_2>
</m:editBroadbandAccountRequest>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>


java代码如下:取根节点的所有属性

Element root = document.getRootElement();
int attrCount = root.attributeCount();
System.out.println("attrCount:::"+attrCount);
for (int i=0; i<attrCount; i++){
Attribute attr = root.attribute(i);
System.out.println(attr.getName());
}

这段代码是用来取xml文档根节点的所有属性,但是现在打印出来的结果是:
attrCount:::1
id

也就是说像根节点的
xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"

这些属性都没取到,请问我如何才能取到这些属性呢?谢谢了。
...全文
301 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
blazingfire 2010-10-15
  • 打赏
  • 举报
回复
取不了吧。
xmlns个是xml命名空间属性,是内置属性。看看,是讲html的,不过xml也差不多
http://www.w3school.com.cn/tags/tag_prop_xmlns.asp
limengchengg 2010-10-15
  • 打赏
  • 举报
回复
属性的取得方法 例如
e.valueOf("@" + 属性名称)
king_spf 2010-10-14
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 blazingfire 的回复:]

Element root = document.getRootElement();
for (Object o : root.declaredNamespaces()) {
Namespace ns = (Namespace) o;
System.out.println("xmlns:" + ns.getPrefix() + "=\"" + ns.getURI() + "\""……
[/Quote]

请问四楼的兄弟,
System.out.println("xmlns:" + ns.getPrefix() + "=\"" + ns.getURI() + "\""……
我如何得到xmlns,而不是直接加上。
Miracle1216 2010-10-13
  • 打赏
  • 举报
回复
declaredNamespaces()要dom4j才有的吧。。。
blazingfire 2010-10-13
  • 打赏
  • 举报
回复
Element root = document.getRootElement();
for (Object o : root.declaredNamespaces()) {
Namespace ns = (Namespace) o;
System.out.println("xmlns:" + ns.getPrefix() + "=\"" + ns.getURI() + "\"");
}
blazingfire 2010-10-13
  • 打赏
  • 举报
回复
Element root = document.getRootElement();
for (Object o : root.declaredNamespaces()) {
Namespace ns = (Namespace) o;
System.out.println(ns.getName() + "=\"" + ns.getURI() + "\"");
}
zidasine 2010-10-13
  • 打赏
  • 举报
回复
root.declaredNamespaces()
yjflinchong 2010-10-13
  • 打赏
  • 举报
回复
for (int i=0; i<attrCount; i++){
Attribute attr = root.attribute(i);
System.out.println(attr.getName());
}

你这里只有getName 啊 。 要getAttribute()吧。。。 用获取属性那个方法。

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧