在Groovy文件中使用Java获取子内部XML标记值

时间:2015-09-23 06:02:33

标签: java xml

我试图获取xml标记值。这里的问题是我在不同的内部标签中有相同的标签。我想获取标签下的标签值。这是代码

<?xml version="1.0" encoding="utf-8"?>
<soapenv:Envelope
xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <soapenv:Body>
        <getResponse
        xmlns="http://bankBazaarService.icicibank.com">
        <getResult>
            <arrayOfContext>
                <item>
                    <errorCode></errorCode>
                    <errorDescription></errorDescription>
                </item>
            </arrayOfContext>
            <typeOfEmploymentDetails>
                <type></type>
                <companyName></companyName>
                <typeOfBusiness></typeOfBusiness>
            </typeOfEmploymentDetails>
            <getOffers>
                <item>
                    <offerId>1</offerId>                        
                </item>
                <item>
                    <offerId>2</offerId>
                </item>
            </getOffers>
            </getResult>
        </getResponse>
    </soapenv:Body>
</soapenv:Envelope>

我的代码是

    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    DocumentBuilder builder;

    builder = factory.newDocumentBuilder();
    Document document = builder.parse( new InputSource( new StringReader( responseXML ) ) );
    NodeList nList = document.getElementsByTagName("item");
    for (int i = 0; i < nList.getLength(); i++) {
        Element element = (Element) nList.item(i);
        println element
        String offerUd = element.getElementsByTagName("offerId").item(0).textContent?:""
        println offerId
    }

元素打印为

<?xml version="1.0" encoding="UTF-8"?><item>
                    <errorCode/>
                    <errorDescription/>
                </item>

我实际上是想要获取                      1
        

这样我就可以获得标签的值。即offerId。代码中是否有任何问题

1 个答案:

答案 0 :(得分:0)

我拿了你的代码,修正了拼写错误(offerUd - &gt; offerId),把它变成了Java:

@Test
public void test() throws ParserConfigurationException, IOException, SAXException
{
  DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
  DocumentBuilder builder;

  builder = factory.newDocumentBuilder();
  org.w3c.dom.Document document = builder.parse(new InputSource(new StringReader(responseXML)));
  NodeList nList = document.getElementsByTagName("item");
  for (int i = 0; i < nList.getLength(); i++)
  {
    Element element = (Element) nList.item(i);
    println(element.toString());
    String offerUd = element.getElementsByTagName("offerId").item(0).getTextContent() != null ? element.getElementsByTagName("offerId").item(0).getTextContent() : "";
    println(offerUd);
  }
}

private void println(String t)
{
  System.out.println(t);
}

我在获取0标记的项"offerId"时遇到了NullPointerException,因为XML中的第一项没有"offerId"标记。

java.lang.NullPointerException
    at xml.ReadXML.test(ReadXML.java:75)

更新:这是工作代码。首先获取GetOffers元素,然后获取其下的项目。

@Test
public void test() throws ParserConfigurationException, IOException, SAXException
{
  DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
  DocumentBuilder builder;

  builder = factory.newDocumentBuilder();
  org.w3c.dom.Document document = builder.parse(new InputSource(new StringReader(responseXML)));
  NodeList offersList = document.getElementsByTagName("getOffers");
  for (int j = 0; j < offersList.getLength(); j++)
  {
    Element offer = (Element) offersList.item(j);

    NodeList nList = offer.getElementsByTagName("item");
    for (int i = 0; i < nList.getLength(); i++)
    {
      Element element = (Element) nList.item(i);

      String offerUd = element.getElementsByTagName("offerId").item(0).getTextContent() != null ? element.getElementsByTagName("offerId").item(0).getTextContent() : "";
      System.out.println(offerUd);
    }
  }
}