当前位置:编程学习 > JAVA >>

DOM4J解析这个XML失败。


<?xml version="1.0" encoding="UTF-8"?>

<GetDanPanHQResponse xmlns="http://tempuri.org/">
  <GetDanPanHQResult>
    <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns="" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata" id="NewDataSet">
      <xs:element name="NewDataSet" msdata:IsDataSet="true" msdata:MainDataTable="DanPanHq" msdata:UseCurrentLocale="true">
        <xs:complexType>
          <xs:choice minOccurs="0" maxOccurs="unbounded">
            <xs:element name="DanPanHq">
              <xs:complexType>
                <xs:sequence>
                  <xs:element name="name" type="xs:string" minOccurs="0"/>
                  <xs:element name="code" type="xs:string" minOccurs="0"/>
                  <xs:element name="close" type="xs:string" minOccurs="0"/>
                  <xs:element name="updown" type="xs:string" minOccurs="0"/>
                  <xs:element name="ratio" type="xs:string" minOccurs="0"/>
                  <xs:element name="amount" type="xs:string" minOccurs="0"/>
                  <xs:element name="station" type="xs:string" minOccurs="0"/>
                  <xs:element name="date" type="xs:string" minOccurs="0"/>
                  <xs:element name="time" type="xs:string" minOccurs="0"/>
                  <xs:element name="upcount" type="xs:string" minOccurs="0"/>
                  <xs:element name="downcount" type="xs:string" minOccurs="0"/>
                  <xs:element name="equalcount" type="xs:string" minOccurs="0"/>
                </xs:sequence>
              </xs:complexType>
            </xs:element>
          </xs:choice>
        </xs:complexType>
      </xs:element>
    </xs:schema>
    <diffgr:diffgram xmlns:diffgr="urn:schemas-microsoft-com:xml-diffgram-v1" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
      <DocumentElement xmlns="">
        <DanPanHq diffgr:id="DanPanHq1" msdata:rowOrder="0" diffgr:hasChanges="inserted">
          <name>上证指数</name>
          <code>000001</code>
          <close>2331.37</close>
          <updown>-46.15</updown>
          <ratio>-1.94%</ratio>
          <amount>578.68亿</amount>
          <station>-1</station>
          <date>2011-10-20</date>
          <time>15:02:53</time>
          <upcount>139</upcount>
          <downcount>1346</downcount>
          <equalcount>354</equalcount>
        </DanPanHq>
        <DanPanHq diffgr:id="DanPanHq2" msdata:rowOrder="1" diffgr:hasChanges="inserted">
          <name>深证成指</name>
          <code>399001</code>
          <close>9796.23</close>
          <updown>-309.51</updown>
          <ratio>-3.06%</ratio>
          <amount>77.43亿</amount>
          <station>-1</station>
          <date>2011-10-20</date>
          <time>15:02:53</time>
          <upcount>128</upcount>
          <downcount>1651</downcount>
          <equalcount>10</equalcount>
        </DanPanHq>
        <DanPanHq diffgr:id="DanPanHq3" msdata:rowOrder="2" diffgr:hasChanges="inserted">
          <name>上证A指</name>
          <code>000002</code>
          <close>2442.03</close>
          <updown>-48.32</updown>
          <ratio>-1.94%</ratio>
          <amount>576.99亿</amount>
          <station>-1</station>
          <date>2011-10-20</date>
          <time>15:02:53</time>
          <upcount>61</upcount>
          <downcount>821</downcount>
          <equalcount>34</equalcount>
        </DanPanHq>
        <DanPanHq diffgr:id="DanPanHq4" msdata:rowOrder="3" diffgr:hasChanges="inserted">
          <name>上证B指</name>
          <code>000003</code>
          <close>234.47</close>
          <updown>-4.85</updown>
          <ratio>-2.03%</ratio>
          <amount>1.69亿</amount>
          <station>-1</station>
          <date>2011-10-20</date>
          <time>15:02:53</time>
          <upcount>1</upcount>
          <downcount>52</downcount>
          <equalcount>0</equalcount>
        </DanPanHq>
        <DanPanHq diffgr:id="DanPanHq5" msdata:rowOrder="4" diffgr:hasChanges="inserted">
          <name>深证A指</name>
          <code>399002</code>
          <close>10741.96</close>
          <updown>-339.39</updown>
          <ratio>-3.06%</ratio>
          <amount>77.43亿</amount>
          <station>-1</station>
          <date>2011-10-20</date>
          <time>15:02:53</time>
          <upcount>85</upcount>
          <downcount>963</downcount>
          <equalcount>7</equalcount>
        </DanPanHq>
        <DanPanHq diffgr:id="DanPanHq6" msdata:rowOrder="5" diffgr:hasChanges="inserted">
          <name>深证B指</name>
          <code>399003</code>
          <close>4116.46</close>
          <updown>-109.45</updown>
          <ratio>-2.59%</ratio>
          <amount>0.50亿</amount>
          <station>-1</station>
          <date>2011-10-20</date>
          <time>15:02:53</time>
          <upcount>3</upcount>
          <downcount>50</downcount>
          <equalcount>0</equalcount>
        </DanPanHq>
        <DanPanHq diffgr:id="DanPanHq7" msdata:rowOrder="6" diffgr:hasChanges="inserted">
          <name>上证50</name>
          <code>000016</code>
          <close>1674.03</close>
          <updown>-34.92</updown>
          <ratio>-2.04%</ratio>
          <amount>144.93亿</amount>
          <station>-1</station>
          <date>2011-10-20</date>
          <time>15:02:53</time>
          <upcount>1</upcount>
          <downcount>47</downcount>
          <equalcount>2</equalcount>
        </DanPanHq>
        <DanPanHq diffgr:id="DanPanHq8" msdata:rowOrder="7" diffgr:hasChanges="inserted">
          <name>沪深300</name>
          <code>399300</code>
          <close>2520.54</close>
          <updown>-62.54</updown>
          <ratio>-2.42%</ratio>
          <amount>392.93亿</amount>
          <station>-1</station>
          <date>2011-10-20</date>
          <time>15:02:53</time>
          <upcount>19</upcount>
          <downcount>269</downcount>
          <equalcount>9</equalcount>
        </DanPanHq>
        <DanPanHq diffgr:id="DanPanHq9" msdata:rowOrder="8" diffgr:hasChanges="inserted">
          <name>创业板指</name>
          <code>399006</code>
          <close>778.95</close>
          <updown>-25.48</updown>
          <ratio>-3.17%</ratio>
          <amount>28.55亿</amount>
          <station>-1</station>
          <date>2011-10-20</date>
          <time>15:02:53</time>
          <upcount>6</upcount>
          <downcount>94</downcount>
          <equalcount>0</equalcount>
        </DanPanHq>
      </DocumentElement>
    </diffgr:diffgram>
  </GetDanPanHQResult>
</GetDanPanHQResponse>


求命名空间核心代码。

byte[] bytes = xmlStr.getBytes();
InputStream in = new ByteArrayInputStream(bytes);
InputStreamReader strInStream = new InputStreamReader(in, "UTF-8");
HashMap xmlMap = new HashMap();
xmlMap.put("xs", "http://tempuri.org/");
reader.getDocumentFactory().setXPathNamespaceURIs(xmlMap); 
document = reader.read(strInStream);
Element element1 = (Element) document
.selectSingleNode("GetDanPanHQResponse/GetDanPanHQResult/xs:schema/xs:element/xs:complexType/xs:choice/xs:element/xs:complexType/xs:sequence");

element1是null。
--------------------编程问答-------------------- 帮顶。

这个网上的教程很多
补充:Java ,  Java SE
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,