根据我的要求,我必须使用以下xml字符串中的标记<Final-Results>...</Final-Results>
内的内容形成一个新的xml。我需要所有标签之间的所有节点,元素及其值。
<?xml version="1.0" encoding="UTF-8"?>
<response>
<response>
<response>
<PQCaseInquiry>
<userID>12345</userID>
<password>XXX</password>
<hostAddress>10.193.236.57</hostAddress>
<hostPort>12955</hostPort>
<fromDate>2014-02-01-00.00.00.000000</fromDate>
<toDate>2014-04-02-23.59.59.999999</toDate>
<FOLDTYPE>GROUP</FOLDTYPE>
<FOLDERID>COMM*H</FOLDERID>
</PQCaseInquiry>
<tempfolder>
<row>
<CRDATTIM>2012-08-31-08.26.11.805400</CRDATTIM>
<RECORDCD>F</RECORDCD>
<CRNODE>01</CRNODE>
<FKEY>2012-08-31-08.26.11.805400F01</FKEY>
<UNITCD>CSMHCQA</UNITCD>
<FOLDTYPE>GROUP</FOLDTYPE>
<FOLDERID>COMM*H</FOLDERID>
<lastCRDATTIM>
<CRDATTIM>2014-03-25-07.23.44.746840</CRDATTIM>
</lastCRDATTIM>
<row>
<row>
<CRDATTIM>2014-03-26-05.22.22.339840</CRDATTIM>
<RECORDCD>C</RECORDCD>
<CRNODE>01</CRNODE>
<CKEY>2014-03-26-05.22.22.339840C01</CKEY>
<STATCD>CREATED</STATCD>
<UNITCD>CSMHCQA</UNITCD>
<WRKTYPE>CALL</WRKTYPE>
<QUEUECD>END</QUEUECD>
<STPTIM>2014-03-26-15.52.21.000000</STPTIM>
<CMOOD>Happy</CMOOD>
<CMEDIA>Phone</CMEDIA>
</row>
<row>
<CRDATTIM>2014-03-26-05.05.51.531840</CRDATTIM>
<RECORDCD>C</RECORDCD>
<CRNODE>01</CRNODE>
<CKEY>2014-03-26-05.05.51.531840C01</CKEY>
<STATCD>CREATED</STATCD>
<UNITCD>CSMHCQA</UNITCD>
<WRKTYPE>CALL</WRKTYPE>
<QUEUECD>END</QUEUECD>
<STPTIM>2014-03-26-15.35.50.000000</STPTIM>
<CMOOD>Happy</CMOOD>
<CMEDIA>Phone</CMEDIA>
</row>
<row>
<CRDATTIM>2014-03-26-05.00.28.231840</CRDATTIM>
<RECORDCD>C</RECORDCD>
<CRNODE>01</CRNODE>
<CKEY>2014-03-26-05.00.28.231840C01</CKEY>
<STATCD>CREATED</STATCD>
<UNITCD>CSMHCQA</UNITCD>
<WRKTYPE>CALL</WRKTYPE>
<QUEUECD>END</QUEUECD>
<STPTIM>2014-03-26-15.30.26.000000</STPTIM>
<CMOOD>Happy</CMOOD>
<CMEDIA>Phone</CMEDIA>
</row>
<row>
<CRDATTIM>2014-03-26-01.44.49.527840</CRDATTIM>
<RECORDCD>C</RECORDCD>
<CRNODE>01</CRNODE>
<CKEY>2014-03-26-01.44.49.527840C01</CKEY>
<STATCD>CREATED</STATCD>
<UNITCD>CSMHCQA</UNITCD>
<WRKTYPE>CALL</WRKTYPE>
<QUEUECD>END</QUEUECD>
<STPTIM>2014-03-26-12.14.48.000000</STPTIM>
<CMOOD>Happy</CMOOD>
<CMEDIA>Phone</CMEDIA>
</row>
<row>
<CRDATTIM>2014-03-26-01.40.52.728840</CRDATTIM>
<RECORDCD>C</RECORDCD>
<CRNODE>01</CRNODE>
<CKEY>2014-03-26-01.40.52.728840C01</CKEY>
<STATCD>CREATED</STATCD>
<UNITCD>CSMHCQA</UNITCD>
<WRKTYPE>CALL</WRKTYPE>
<QUEUECD>END</QUEUECD>
<STPTIM>2014-03-26-12.10.51.000000</STPTIM>
<CMOOD>Happy</CMOOD>
<CMEDIA>Phone</CMEDIA>
</row>
<row>
<CRDATTIM>2014-03-26-01.36.16.402840</CRDATTIM>
<RECORDCD>C</RECORDCD>
<CRNODE>01</CRNODE>
<CKEY>2014-03-26-01.36.16.402840C01</CKEY>
<STATCD>CREATED</STATCD>
<UNITCD>CSMHCQA</UNITCD>
<WRKTYPE>CALL</WRKTYPE>
<QUEUECD>END</QUEUECD>
<STPTIM>2014-03-26-12.06.15.000000</STPTIM>
<CMOOD>Happy</CMOOD>
<CMEDIA>Phone</CMEDIA>
</row>
<row>
<CRDATTIM>2014-03-26-01.34.17.928840</CRDATTIM>
<RECORDCD>C</RECORDCD>
<CRNODE>01</CRNODE>
<CKEY>2014-03-26-01.34.17.928840C01</CKEY>
<STATCD>CREATED</STATCD>
<UNITCD>CSMHCQA</UNITCD>
<WRKTYPE>CALL</WRKTYPE>
<QUEUECD>END</QUEUECD>
<STPTIM>2014-03-26-12.04.16.000000</STPTIM>
<CMOOD>Happy</CMOOD>
<CMEDIA>Phone</CMEDIA>
</row>
<row>
<CRDATTIM>2014-03-25-07.55.16.612840</CRDATTIM>
<RECORDCD>C</RECORDCD>
<CRNODE>01</CRNODE>
<CKEY>2014-03-25-07.55.16.612840C01</CKEY>
<STATCD>CREATED</STATCD>
<UNITCD>CSMHCQA</UNITCD>
<WRKTYPE>CALL</WRKTYPE>
<QUEUECD>END</QUEUECD>
<STPTIM>2014-03-25-18.25.15.000000</STPTIM>
<CMOOD>Happy</CMOOD>
<CMEDIA>Phone</CMEDIA>
</row>
<row>
<CRDATTIM>2014-03-25-07.35.15.992840</CRDATTIM>
<RECORDCD>C</RECORDCD>
<CRNODE>01</CRNODE>
<CKEY>2014-03-25-07.35.15.992840C01</CKEY>
<STATCD>CREATED</STATCD>
<UNITCD>CSMHCQA</UNITCD>
<WRKTYPE>CALL</WRKTYPE>
<QUEUECD>END</QUEUECD>
<STPTIM>2014-03-25-18.05.14.000000</STPTIM>
<CMOOD>Happy</CMOOD>
<CMEDIA>Phone</CMEDIA>
</row>
<row>
<CRDATTIM>2014-03-25-07.23.44.746840</CRDATTIM>
<RECORDCD>C</RECORDCD>
<CRNODE>01</CRNODE>
<CKEY>2014-03-25-07.23.44.746840C01</CKEY>
<STATCD>CREATED</STATCD>
<UNITCD>CSMHCQA</UNITCD>
<WRKTYPE>CALL</WRKTYPE>
<QUEUECD>END</QUEUECD>
<STPTIM>2014-03-25-17.53.43.000000</STPTIM>
<CMOOD>Happy</CMOOD>
<CMEDIA>Phone</CMEDIA>
</row>
</row>
</row>
</tempfolder>
<TempFolders>
<row>
<CRDATTIM>2012-11-06-23.57.08.089400</CRDATTIM>
<RECORDCD>F</RECORDCD>
<CRNODE>01</CRNODE>
<FKEY>2012-11-06-23.57.08.089400F01</FKEY>
<UNITCD>CSMHCDV</UNITCD>
<FOLDTYPE>GROUP</FOLDTYPE>
<FOLDERID>COMM*H</FOLDERID>
</row>
</TempFolders>
<CaseCount>10</CaseCount>
</response>
<results>
<PQCLKUP-Results>
<row>
<CRDATTIM>2014-03-26-05.22.22.339840</CRDATTIM>
<RECORDCD>C</RECORDCD>
<CRNODE>01</CRNODE>
<CKEY>2014-03-26-05.22.22.339840C01</CKEY>
<STATCD>CREATED</STATCD>
<UNITCD>CSMHCQA</UNITCD>
<WRKTYPE>CALL</WRKTYPE>
<QUEUECD>END</QUEUECD>
<STPTIM>2014-03-26-15.52.21.000000</STPTIM>
<CMOOD>Happy</CMOOD>
<CMEDIA>Phone</CMEDIA>
</row>
<row>
<CRDATTIM>2014-03-26-05.05.51.531840</CRDATTIM>
<RECORDCD>C</RECORDCD>
<CRNODE>01</CRNODE>
<CKEY>2014-03-26-05.05.51.531840C01</CKEY>
<STATCD>CREATED</STATCD>
<UNITCD>CSMHCQA</UNITCD>
<WRKTYPE>CALL</WRKTYPE>
<QUEUECD>END</QUEUECD>
<STPTIM>2014-03-26-15.35.50.000000</STPTIM>
<CMOOD>Happy</CMOOD>
<CMEDIA>Phone</CMEDIA>
</row>
<row>
<CRDATTIM>2014-03-26-05.00.28.231840</CRDATTIM>
<RECORDCD>C</RECORDCD>
<CRNODE>01</CRNODE>
<CKEY>2014-03-26-05.00.28.231840C01</CKEY>
<STATCD>CREATED</STATCD>
<UNITCD>CSMHCQA</UNITCD>
<WRKTYPE>CALL</WRKTYPE>
<QUEUECD>END</QUEUECD>
<STPTIM>2014-03-26-15.30.26.000000</STPTIM>
<CMOOD>Happy</CMOOD>
<CMEDIA>Phone</CMEDIA>
</row>
<row>
<CRDATTIM>2014-03-26-01.44.49.527840</CRDATTIM>
<RECORDCD>C</RECORDCD>
<CRNODE>01</CRNODE>
<CKEY>2014-03-26-01.44.49.527840C01</CKEY>
<STATCD>CREATED</STATCD>
<UNITCD>CSMHCQA</UNITCD>
<WRKTYPE>CALL</WRKTYPE>
<QUEUECD>END</QUEUECD>
<STPTIM>2014-03-26-12.14.48.000000</STPTIM>
<CMOOD>Happy</CMOOD>
<CMEDIA>Phone</CMEDIA>
</row>
<row>
<CRDATTIM>2014-03-26-01.40.52.728840</CRDATTIM>
<RECORDCD>C</RECORDCD>
<CRNODE>01</CRNODE>
<CKEY>2014-03-26-01.40.52.728840C01</CKEY>
<STATCD>CREATED</STATCD>
<UNITCD>CSMHCQA</UNITCD>
<WRKTYPE>CALL</WRKTYPE>
<QUEUECD>END</QUEUECD>
<STPTIM>2014-03-26-12.10.51.000000</STPTIM>
<CMOOD>Happy</CMOOD>
<CMEDIA>Phone</CMEDIA>
</row>
<row>
<CRDATTIM>2014-03-26-01.36.16.402840</CRDATTIM>
<RECORDCD>C</RECORDCD>
<CRNODE>01</CRNODE>
<CKEY>2014-03-26-01.36.16.402840C01</CKEY>
<STATCD>CREATED</STATCD>
<UNITCD>CSMHCQA</UNITCD>
<WRKTYPE>CALL</WRKTYPE>
<QUEUECD>END</QUEUECD>
<STPTIM>2014-03-26-12.06.15.000000</STPTIM>
<CMOOD>Happy</CMOOD>
<CMEDIA>Phone</CMEDIA>
</row>
<row>
<CRDATTIM>2014-03-26-01.34.17.928840</CRDATTIM>
<RECORDCD>C</RECORDCD>
<CRNODE>01</CRNODE>
<CKEY>2014-03-26-01.34.17.928840C01</CKEY>
<STATCD>CREATED</STATCD>
<UNITCD>CSMHCQA</UNITCD>
<WRKTYPE>CALL</WRKTYPE>
<QUEUECD>END</QUEUECD>
<STPTIM>2014-03-26-12.04.16.000000</STPTIM>
<CMOOD>Happy</CMOOD>
<CMEDIA>Phone</CMEDIA>
</row>
<row>
<CRDATTIM>2014-03-25-07.55.16.612840</CRDATTIM>
<RECORDCD>C</RECORDCD>
<CRNODE>01</CRNODE>
<CKEY>2014-03-25-07.55.16.612840C01</CKEY>
<STATCD>CREATED</STATCD>
<UNITCD>CSMHCQA</UNITCD>
<WRKTYPE>CALL</WRKTYPE>
<QUEUECD>END</QUEUECD>
<STPTIM>2014-03-25-18.25.15.000000</STPTIM>
<CMOOD>Happy</CMOOD>
<CMEDIA>Phone</CMEDIA>
</row>
<row>
<CRDATTIM>2014-03-25-07.35.15.992840</CRDATTIM>
<RECORDCD>C</RECORDCD>
<CRNODE>01</CRNODE>
<CKEY>2014-03-25-07.35.15.992840C01</CKEY>
<STATCD>CREATED</STATCD>
<UNITCD>CSMHCQA</UNITCD>
<WRKTYPE>CALL</WRKTYPE>
<QUEUECD>END</QUEUECD>
<STPTIM>2014-03-25-18.05.14.000000</STPTIM>
<CMOOD>Happy</CMOOD>
<CMEDIA>Phone</CMEDIA>
</row>
<row>
<CRDATTIM>2014-03-25-07.23.44.746840</CRDATTIM>
<RECORDCD>C</RECORDCD>
<CRNODE>01</CRNODE>
<CKEY>2014-03-25-07.23.44.746840C01</CKEY>
<STATCD>CREATED</STATCD>
<UNITCD>CSMHCQA</UNITCD>
<WRKTYPE>CALL</WRKTYPE>
<QUEUECD>END</QUEUECD>
<STPTIM>2014-03-25-17.53.43.000000</STPTIM>
<CMOOD>Happy</CMOOD>
<CMEDIA>Phone</CMEDIA>
</row>
</PQCLKUP-Results>
<cases>
<row>
<CRDATTIM>2014-03-26-05.05.51.531840</CRDATTIM>
<RECORDCD>C</RECORDCD>
<CRNODE>01</CRNODE>
<CKEY>2014-03-26-05.05.51.531840C01</CKEY>
<STATCD>CREATED</STATCD>
<UNITCD>CSMHCQA</UNITCD>
<WRKTYPE>CALL</WRKTYPE>
<QUEUECD>END</QUEUECD>
<STPTIM>2014-03-26-15.35.50.000000</STPTIM>
<CMOOD>Happy</CMOOD>
<CMEDIA>Phone</CMEDIA>
</row>
<row>
<CRDATTIM>2014-03-26-05.00.28.231840</CRDATTIM>
<RECORDCD>C</RECORDCD>
<CRNODE>01</CRNODE>
<CKEY>2014-03-26-05.00.28.231840C01</CKEY>
<STATCD>CREATED</STATCD>
<UNITCD>CSMHCQA</UNITCD>
<WRKTYPE>CALL</WRKTYPE>
<QUEUECD>END</QUEUECD>
<STPTIM>2014-03-26-15.30.26.000000</STPTIM>
<CMOOD>Happy</CMOOD>
<CMEDIA>Phone</CMEDIA>
</row>
<row>
<CRDATTIM>2014-03-26-01.44.49.527840</CRDATTIM>
<RECORDCD>C</RECORDCD>
<CRNODE>01</CRNODE>
<CKEY>2014-03-26-01.44.49.527840C01</CKEY>
<STATCD>CREATED</STATCD>
<UNITCD>CSMHCQA</UNITCD>
<WRKTYPE>CALL</WRKTYPE>
<QUEUECD>END</QUEUECD>
<STPTIM>2014-03-26-12.14.48.000000</STPTIM>
<CMOOD>Happy</CMOOD>
<CMEDIA>Phone</CMEDIA>
</row>
<row>
<CRDATTIM>2014-03-26-01.40.52.728840</CRDATTIM>
<RECORDCD>C</RECORDCD>
<CRNODE>01</CRNODE>
<CKEY>2014-03-26-01.40.52.728840C01</CKEY>
<STATCD>CREATED</STATCD>
<UNITCD>CSMHCQA</UNITCD>
<WRKTYPE>CALL</WRKTYPE>
<QUEUECD>END</QUEUECD>
<STPTIM>2014-03-26-12.10.51.000000</STPTIM>
<CMOOD>Happy</CMOOD>
<CMEDIA>Phone</CMEDIA>
</row>
<row>
<CRDATTIM>2014-03-26-01.36.16.402840</CRDATTIM>
<RECORDCD>C</RECORDCD>
<CRNODE>01</CRNODE>
<CKEY>2014-03-26-01.36.16.402840C01</CKEY>
<STATCD>CREATED</STATCD>
<UNITCD>CSMHCQA</UNITCD>
<WRKTYPE>CALL</WRKTYPE>
<QUEUECD>END</QUEUECD>
<STPTIM>2014-03-26-12.06.15.000000</STPTIM>
<CMOOD>Happy</CMOOD>
<CMEDIA>Phone</CMEDIA>
</row>
<row>
<CRDATTIM>2014-03-26-01.34.17.928840</CRDATTIM>
<RECORDCD>C</RECORDCD>
<CRNODE>01</CRNODE>
<CKEY>2014-03-26-01.34.17.928840C01</CKEY>
<STATCD>CREATED</STATCD>
<UNITCD>CSMHCQA</UNITCD>
<WRKTYPE>CALL</WRKTYPE>
<QUEUECD>END</QUEUECD>
<STPTIM>2014-03-26-12.04.16.000000</STPTIM>
<CMOOD>Happy</CMOOD>
<CMEDIA>Phone</CMEDIA>
</row>
<row>
<CRDATTIM>2014-03-25-07.55.16.612840</CRDATTIM>
<RECORDCD>C</RECORDCD>
<CRNODE>01</CRNODE>
<CKEY>2014-03-25-07.55.16.612840C01</CKEY>
<STATCD>CREATED</STATCD>
<UNITCD>CSMHCQA</UNITCD>
<WRKTYPE>CALL</WRKTYPE>
<QUEUECD>END</QUEUECD>
<STPTIM>2014-03-25-18.25.15.000000</STPTIM>
<CMOOD>Happy</CMOOD>
<CMEDIA>Phone</CMEDIA>
</row>
<row>
<CRDATTIM>2014-03-25-07.35.15.992840</CRDATTIM>
<RECORDCD>C</RECORDCD>
<CRNODE>01</CRNODE>
<CKEY>2014-03-25-07.35.15.992840C01</CKEY>
<STATCD>CREATED</STATCD>
<UNITCD>CSMHCQA</UNITCD>
<WRKTYPE>CALL</WRKTYPE>
<QUEUECD>END</QUEUECD>
<STPTIM>2014-03-25-18.05.14.000000</STPTIM>
<CMOOD>Happy</CMOOD>
<CMEDIA>Phone</CMEDIA>
</row>
<row>
<CRDATTIM>2014-03-25-07.23.44.746840</CRDATTIM>
<RECORDCD>C</RECORDCD>
<CRNODE>01</CRNODE>
<CKEY>2014-03-25-07.23.44.746840C01</CKEY>
<STATCD>CREATED</STATCD>
<UNITCD>CSMHCQA</UNITCD>
<WRKTYPE>CALL</WRKTYPE>
<QUEUECD>END</QUEUECD>
<STPTIM>2014-03-25-17.53.43.000000</STPTIM>
<CMOOD>Happy</CMOOD>
<CMEDIA>Phone</CMEDIA>
</row>
</cases>
<KEY>
<CKEY>2014-03-26-05.22.22.339840C01</CKEY>
<CKEY>2014-03-26-05.05.51.531840C01</CKEY>
<CKEY>2014-03-26-05.00.28.231840C01</CKEY>
<CKEY>2014-03-26-01.44.49.527840C01</CKEY>
<CKEY>2014-03-26-01.40.52.728840C01</CKEY>
<CKEY>2014-03-26-01.36.16.402840C01</CKEY>
<CKEY>2014-03-26-01.34.17.928840C01</CKEY>
<CKEY>2014-03-25-07.55.16.612840C01</CKEY>
<CKEY>2014-03-25-07.35.15.992840C01</CKEY>
<CKEY>2014-03-25-07.23.44.746840C01</CKEY>
</KEY>
</results>
<Final-Results>
<row>
<CRDATTIM>2014-03-26-05.22.22.339840</CRDATTIM>
<RECORDCD>C</RECORDCD>
<CRNODE>01</CRNODE>
<CKEY>2014-03-26-05.22.22.339840C01</CKEY>
<STATCD>CREATED</STATCD>
<UNITCD>CSMHCQA</UNITCD>
<WRKTYPE>CALL</WRKTYPE>
<QUEUECD>END</QUEUECD>
<STPTIM>2014-03-26-15.52.21.000000</STPTIM>
<CMOOD>Happy</CMOOD>
<CMEDIA>Phone</CMEDIA>
<issues>
<row>
<IKEY>2014-03-26-05.22.22.193840T01</IKEY>
<PRTY>999</PRTY>
<ISSUEID>20140326-155047-DT81694</ISSUEID>
<SUBJECT>Group</SUBJECT>
<ISSTYP>GROUP</ISSTYP>
<ISSCAT1>GROUP INQUIRY</ISSCAT1>
<RELMEM />
<RELGRP>COMM*H</RELGRP>
<RELPRV />
<UNITCD>CSMHCQA</UNITCD>
<WRKTYPE>ISSUE</WRKTYPE>
<STATCD>CATCHALL</STATCD>
<QUEUECD>CATCHALL</QUEUECD>
<CRDATTIM>2014-03-26-05.22.22.193840</CRDATTIM>
<LOCKSTAT />
<ORIGUSERID>DT81694</ORIGUSERID>
<STPTIM>2014-03-26-15.50.47.000000</STPTIM>
<RELPRVTYP />
<RELCLM />
<RELCLMBEGDOS />
<RELCLMENDDOS />
<RELAUTH />
<RELAUTHBEGDOS />
<RELAUTHENDDOS />
<ST_CHG_DATTIM>2014-03-26-05.24.59.464840</ST_CHG_DATTIM>
<END_QUEUE_FLG>N</END_QUEUE_FLG>
<ASSIGNID />
<LOCKWORK_FLG />
</row>
</issues>
</row>
</Final-Results>
<CaseCount>10</CaseCount>
</response>
<results>
<row>
<IKEY>2014-03-26-05.22.22.193840T01</IKEY>
<PRTY>999</PRTY>
<ISSUEID>20140326-155047-DT81694</ISSUEID>
<SUBJECT>Group</SUBJECT>
<ISSTYP>GROUP</ISSTYP>
<ISSCAT1>GROUP INQUIRY</ISSCAT1>
<RELMEM />
<RELGRP>COMM*H</RELGRP>
<RELPRV />
<UNITCD>CSMHCQA</UNITCD>
<WRKTYPE>ISSUE</WRKTYPE>
<STATCD>CATCHALL</STATCD>
<QUEUECD>CATCHALL</QUEUECD>
<CRDATTIM>2014-03-26-05.22.22.193840</CRDATTIM>
<LOCKSTAT />
<ORIGUSERID>DT81694</ORIGUSERID>
<STPTIM>2014-03-26-15.50.47.000000</STPTIM>
<RELPRVTYP />
<RELCLM />
<RELCLMBEGDOS />
<RELCLMENDDOS />
<RELAUTH />
<RELAUTHBEGDOS />
<RELAUTHENDDOS />
<ST_CHG_DATTIM>2014-03-26-05.24.59.464840</ST_CHG_DATTIM>
<END_QUEUE_FLG>N</END_QUEUE_FLG>
<ASSIGNID />
<LOCKWORK_FLG />
</row>
</results>
</response>
我尝试过使用以下Java代码:
XPath xPath = XPathFactory.newInstance().newXPath();
org.w3c.dom.NodeList titleNodes = (NodeList) xPath.evaluate("//Final-Results", xmlString, XPathConstants.NODESET);
我使用上面的代码得到以下异常:
javax.xml.transform.TransformerException: Unable to evaluate expression using this context
我在这里做错了吗?如果我的方法有误,你能否用一些伪代码分享你的想法以实现我的功能?
答案 0 :(得分:2)
您正在尝试执行以下操作:
NodeList list = (NodeList) XPathFactory.newInstance()
.newXPath()
.evaluate("/xml", "<xml />", XPathConstants.NODESET);
但是, String 是not a supported argument。实现期望Node。提供InputSource:
Reader xml = new StringReader("<xml />");
InputSource src = new InputSource(xml);
NodeList list = (NodeList) XPathFactory.newInstance()
.newXPath()
.evaluate("/xml", src, XPathConstants.NODESET);
假设XML是一个文件,提供二进制 InputStream 并让XML解析器检测字符编码。
答案 1 :(得分:1)
Document doc = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse("test.xml");
XPath xPath = XPathFactory.newInstance().newXPath();
org.w3c.dom.NodeList titleNodes = (NodeList) xPath.evaluate("//tempfolder", doc,XPathConstants.NODESET);
TransformerFactory transformerFactory = TransformerFactory.newInstance();
Transformer transformer = transformerFactory.newTransformer();
StreamResult result = new StreamResult(new File("out.xml"));
for(int i=0;i<titleNodes.getLength();i++){
Node node = titleNodes.item(i);
DOMSource source = new DOMSource(node);
transformer.transform(source, result);
}
答案 2 :(得分:0)
我尝试了几种方法。但是,以下代码对我有用:
import java.io.IOException;
import java.io.Reader;
import java.io.StringReader;
import java.util.List;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.xpath.XPathExpressionException;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
import org.xml.sax.SAXException;
Reader xml = new StringReader("<xml />");
Document document = null;
SAXReader reader = new SAXReader();
document = reader.read(xml);
List <Element> elements = getXmlElements(document,"//Final-Results");
for(int index = 0; index < elements.size();index ++){
Element element = elements.get(index);
System.out.println("The required xml "+element.asXML())
}
getXmlElements()方法在这里:
private List<Element> getXmlElements(Document document,String xPath) {
List<Element> elements = document.selectNodes(xPath);
return elements;
}