java xml 判断是否有节点_java 判断两个xml文件节点是否相同

public String getReturnData(String urlString) throws UnsupportedEncodingException { 51Testing软件测试网rc|Q S qf K"s

String res = "";

)O"JF]7U}0         try {

oL`1fqY0               URL url = new URL(urlString); 51Testing软件测试网j:xUBS\hq

java.net.HttpURLConnection conn = (java.net.HttpURLConnection)url.openConnection();

*_'i)tK#j}wsr0              conn.setDoOutput(true);               51Testing软件测试网!R(H,V$bE4uU$Ai

conn.setRequestMethod("GET"); 51Testing软件测试网)j$l?s%T}

BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream(),"UTF-8")); 51Testing软件测试网&zF/].b(B?c

String line; 51Testing软件测试网.f6fqfJ8Mw

while ((line = in.readLine()) != null) { 51Testing软件测试网+`{F:PO4_a

res += line;

4Vu(er9YQsR0              } 51Testing软件测试网6^U,|3s9{IdAT

in.close();

-g"k!G,?B6NyL0           } catch (Exception e) {

S%Q%V`,` oDvqm0            System.out.println("出错啦");

C4ijiD~1`v-T0

!Zd(^Y2{0           } 51Testing软件测试网,`fU:JPxz w[

System.out.println(res);

.A8O#u CW4Y3pG0          return res;

b3z$KaZ0M |5~g?0       }

#P"x2c1I0B0  //将string类型转为Document Xml类型51Testing软件测试网A3`4qdua

^]n*eI0  public  Document string2Document(String s)

z6Y!]

tz!KC3Bz D0     {51Testing软件测试网o{:tA4YUzC

Document document = null;51Testing软件测试网:Gc%FcP0aj

try

q/Hm\I&V0        {

$h

U4g9D&ZJv7|E0            DocumentBuilder parser = DocumentBuilderFactory.newInstance().newDocumentBuilder();

y3hhy&b0        //    document = (Document) parser.parse( new InputSource(new StringReader(s)) );51Testing软件测试网]\^Q\"@k$a

document = parser.parse(new InputSource(new StringReader(s)));

+f8\.}

t X$n_0        }catch(Exception ex)

_UvI4b,pU0        {            51Testing软件测试网4qfXz(f2ooas

System.out.println("出错啦");

^hT6wCAn%`@$~0

.e#^5}L!M9G1EgQ0        }51Testing软件测试网+[[l["ht_,[b

return document;

8A[(N~}c0     } 51Testing软件测试网

}6P%nKmPo%T

7dlJ:reAT0  //将xml格式的字符串保存为本地文件,如果字符串格式不符合xml规则,则返回失败51Testing软件测试网&IEMb&W(\h

public  boolean doc2XmlFile(Document document,String filename)51Testing软件测试网/wBZ0`9A"AEC9w

{51Testing软件测试网v0V/Yetp

boolean flag = true;51Testing软件测试网6{6oG4vq%O

try51Testing软件测试网sF8sKj:ci&H8gSw(d2R

{51Testing软件测试网9Cl:rB|/R

TransformerFactory tFactory = TransformerFactory.newInstance();

a2[/@~u&u0              Transformer transformer = tFactory.newTransformer(); 51Testing软件测试网WzQQ`J"Y

DOMSource source = new DOMSource((Node) document);

Uv7i J3E3EW~z0              StreamResult result = new StreamResult(new File(filename));

X`3On5y!lk0              transformer.transform(source, result);51Testing软件测试网KoQ

Ft7V4?z

}catch(Exception ex)

X)Wf.s:\'u&R0          {51Testing软件测试网|q:S,n\.u/L'{w

flag = false;51Testing软件测试网7K'oFe'a%y

System.out.println("出错啦");51Testing软件测试网'@0R`sr

System.out.println("test");

Q7D%u3[l)bY1j0          }

._)XfE%u1xq

]7c @.S7s0         return flag;      51Testing软件测试网-@Ia-hhw

}

x(Qn9X$H9tV0 //将string型转为xml文件并写入本地

]5y8h*S\+h+t4Z0  public boolean string2XmlFile(String str,String filename)51Testing软件测试网"zm)i"VFI

x!~

{51Testing软件测试网lHL'Q!yJ7n2lcvWl

boolean flag = true;51Testing软件测试网l/a;e:Z0\+?"y2l&NH,c

try51Testing软件测试网2xbO%f]%C;I#Uw

{

'?cms/uD.]0           Document doc = string2Document(str);       51Testing软件测试网`2V6LuubtO#y

flag = doc2XmlFile(doc,filename);

rs.m+sRm;pW"Q0        }catch (Exception ex)51Testing软件测试网PJu7}#z7X*L(`%v

{51Testing软件测试网b4H+?)M:U

flag = false;

qu_'W6joM0           ex.printStackTrace();51Testing软件测试网:K&{!grfk.E%O(l

}

m7l#ka|$~zJ0       return flag;

m x3dW]?+pO.~*E0     }

~~r/Gl0 51Testing软件测试网.`/`i ?C

//对比两个xml文件的节点是否相同51Testing软件测试网s{U_!{*]

public  boolean xmlReadDemo(String srcfilename,String compfilename) throws ParserConfigurationException, SAXException, IOException51Testing软件测试网S%B*t#uy-Sdx9qy

{51Testing软件测试网"D.?f[ M^o$J

boolean flag=true;51Testing软件测试网V$ys5J1F

DocumentBuilderFactory   factory = DocumentBuilderFactory.newInstance(); 51Testing软件测试网sKZS4{l

DocumentBuilder builder=factory.newDocumentBuilder();

Q6aK ??Gx0         Document compdocument=builder.parse(new File(compfilename));

6Ciu,V.]4x({Zs0         Document document=builder.parse(new File(srcfilename)); 51Testing软件测试网~&s4Kg;vv

Y2G

document.normalize();51Testing软件测试网!F_nKKO

compdocument.normalize();51Testing软件测试网'o,v

o~uB0Qx

Node srcroot=document.getDocumentElement();

CA|_%Y%S%tHB0      Node comproot=compdocument.getDocumentElement();

D0|b!?i2rme0 //     System.out.println(srcroot);

d/G'wW fp0 //     System.out.println(comproot);51Testing软件测试网%RJ,S3m2eh5g

String src=srcroot.toString();

5XcAG#bW#F*CV0      String comp=comproot.toString();

$?R-e8s+USDV0      if(!(src.equals(comp)))

V|;Jz&wx1i&C%}2L0      { tips="xml文件根结点不相同,出现错误";

\|$r+FS?W)X0      flag=false;51Testing软件测试网{ q;_2lgE9e

System.out.println("xml文件根结点不相同,出现错误");51Testing软件测试网 V D(hb;lL*l

VQ

System.out.println(flag);}51Testing软件测试网`*s}c:A?

else{51Testing软件测试网?/s+D0H^'M%nEAI-S

if(srcroot.hasChildNodes())51Testing软件测试网;Js ~;G3f

{51Testing软件测试网"m#l.g1q7[

NodeList srcnodes = srcroot.getChildNodes();51Testing软件测试网MA{sR

NodeList compnodes = comproot.getChildNodes();

w.L(X:RFJs0     //      System.out.println(srcnodes);

6cu1H#\)Q0     //      System.out.println(compnodes);

E&U2PZp\ce4t0           if(srcnodes.getLength()==compnodes.getLength())

Iz.Jl _0           {

p(oq-Hs]J0          for (int i=0;i

h+^4?)Gw`0           {51Testing软件测试网&M;@^*j{!b

AF$g)]Pap0              NodeList srclist = srcnodes.item(i).getChildNodes();

Fz |(@t0              NodeList complist = compnodes.item(i).getChildNodes();51Testing软件测试网%?2Q8IN(Nh5H/q%R.]

//    System.out.println(srclist.toString());

?8G?C,aN&x%J\0            //  System.out.println(complist.toString());51Testing软件测试网g4`V%H;L;g:GDj

if(!(srclist.toString().equals(complist.toString())))51Testing软件测试网1DH,VK.?Ok

{51Testing软件测试网WD0G_6YTC/g+k

//    System.out.println(srclist.toString());51Testing软件测试网fO&tT j#Q5q+Yen4y)T

//    System.out.println(complist.toString());51Testing软件测试网'?J5}#Ga9J\n0Zsq

tips="两个xml文件的第二层字节点内容不相等,出现错误";51Testing软件测试网+m_0c$lUf8Q;{

System.out.println("两个xml文件的第二层字节点内容不相等,出现错误");51Testing软件测试网n/sSf?v+s

flag=false;51Testing软件测试网$}!N(Gw0^0f2YysT7c

break;}

3C7AaL_`0              else51Testing软件测试网 I{z+{5p

for (int k=0;k

{

NkE*xAX!n*Dm0                Node srcnode = srclist.item(k);51Testing软件测试网}VND1upF

Node compnode = complist.item(k);

5@p)G"yHqM0            //    System.out.println(srcnode);

a3G_0l-`O?xG&i0            //  System.out.println(compnode);51Testing软件测试网%aRdVC _2x

if (srcnode.toString().equals(compnode.toString()))

2RJ%s

t?9S;w.f^0                {

LONN^Y/`2S0            //    System.out.println("test");

;G s YV^,oY7B%k/C{0                }

5u(P2r3x&rZ,?|0               else{

S]qxkw,b0                tips="两个xml叶子节点出现错误";51Testing软件测试网

T/@CB]e;Pe.u

System.out.println("两个xml叶子节点出现错误");51Testing软件测试网6FRe"}+ayB

flag=false;

T8SZCKs\0            break;}51Testing软件测试网P&G/lnq0ck`&l;[H

}

NLHc*efPok0         //     System.out.println("两个xml节点相同");

+[.o~#h:IL9D0             51Testing软件测试网;}(wg7G@

}

To8y(aKT0         51Testing软件测试网y.X\QQh:Gmk4_

}51Testing软件测试网6rW N'Wgi

else{

e-Um|oyG0            tips="两个xml文件子节点个数不相同,出现错误";

5x+`FRzEY`0            System.out.println("两个xml文件子节点个数不相同,出现错误");51Testing软件测试网#v1miK/PA3YB

}51Testing软件测试网6^-H6Btc*PB]

}51Testing软件测试网M T4I`q

}51Testing软件测试网zD@_6mu6sha

MG\X&BM x-_0      return flag;51Testing软件测试网0fG,tj!d^(@jU

51Testing软件测试网+EYz;R2`2H}

}

B.N7DH'm%j0c y2r0

q8C'AZ!{L:~1v0 51Testing软件测试网

t V!\ak

Eb

51Testing软件测试网-r8K [;d

]vd

51Testing软件测试网2]N0_{I/kL]

/* public static void main(String[] args) throws SAXException, IOException, ParserConfigurationException  {51Testing软件测试网b0Ls:\-]b?X}3`

String str="http://platform.aimersoft.com/interface.php?m=suit&product_id=495";51Testing软件测试网tqM0IZO;c"X

String getxml = null;                  //存储返回的xml文件内容

$yo.lZ:e0~bV+@^@0   UrlTest urltest=new UrlTest();

3}]3}(~2JX0  getxml=urltest.getReturnData(str);

'D)Q uL#V#[A0  urltest.string2XmlFile(getxml,"1.xml");

A{|9Y1u d0  boolean flag=urltest.xmlReadDemo("1.xml","2.xml");51Testing软件测试网jeqM_2Y'E2T

if(flag)51Testing软件测试网F9D2yf@g"R3Gw+n

System.out.println("两个节点内容相同");

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值