2010-10-08 126 views
11
<?xml version="1.0" encoding="utf-8"?><soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"><soap:Body><LoginResponse xmlns="http://tempuri.org/QuestIPhoneWebService/QuestIPhoneWebService"><LoginResult>&lt;RETURN_VALUE&gt;&lt;ERROR RESULT= '-1' DESC = 'The password entered into the system is not valid. Please check your password and try again.'/&gt;&lt;/RETURN_VALUE&gt;</LoginResult></LoginResponse></soap:Body></soap:Envelope> 

您好我正在从webservices获取值。我想上面的字符串到XML转换有谁能够告诉你怎么字符串转换为XML文件中的Java如何将字符串转换为java中的xml文件

<?xml version="1.0" encoding="utf-8"?> 
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"> 
<soap:Body> 
<LoginResponse xmlns="http://tempuri.org/QuestIPhoneWebService/QuestIPhoneWebService"> 
<LoginResult> 
&lt;ROOT xmlns:sql="urn:schemas-microsoft-com:xml-sql"&gt;&lt;LOGIN_DETAILS USER_ID="testpub2" COMPANY_ID="1" USER_NAME=" aaa" SYSTEM_USER_ID="6976" USER_EMAIL_ID="[email protected]" TOKEN_STRING="A93805F1F1C340F5A8155FDD9B77E595" DISCLAIMER_AGREED="1" USER_ENABLED="1" USER_COMPANY_ENABLED="1" USER_TYPE="2" LOGIN_EXPIRY_DAYS="999" TOKEN_CREATION_DATE="2010-10-01T16:04:26" MOBILE_ENABLED="1" USER_COMPANY_MOBILE_ENABLED="1"/&gt;&lt;COMPANY_DETAILS CLIENT_TYPE_ID="8"/&gt;&lt;USER_SETTINGS&gt;&lt;QUEST_GROUP ID="14293" NAME="World" ASSIGN_NUM="14"/&gt;&lt;INDEX_PROVIDER ID="14251" NAME="QUEST (Default)"/&gt;&lt;STOCK_IDENTIFIER ID="57" NAME="TICKER"/&gt;&lt;/USER_SETTINGS&gt;&lt;PERMISSIONS&gt;&lt;QUEST_FUNCTIONS&gt;&lt;FUNCTION NAME="charting" ID="501" ACCESS="1"/&gt;&lt;FUNCTION NAME="modeller" ID="512" ACCESS="1"/&gt;&lt;FUNCTION NAME="momentum" ID="513" ACCESS="1"/&gt;&lt;FUNCTION NAME="portfolio" ID="516" ACCESS="1"/&gt;&lt;FUNCTION NAME="search" ID="518" ACCESS="1"/&gt;&lt;FUNCTION NAME="sensitivity" ID="521" ACCESS="1"/&gt;&lt;FUNCTION NAME="statistics" ID="524" ACCESS="1"/&gt;&lt;FUNCTION NAME="strategy" ID="525" ACCESS="1"/&gt;&lt;FUNCTION NAME="summary" ID="526" ACCESS="1"/&gt;&lt;FUNCTION NAME="triangle" ID="528" ACCESS="1"/&gt;&lt;FUNCTION NAME="valuation" ID="529" ACCESS="1"/&gt;&lt;FUNCTION NAME="commentary" ID="530" ACCESS="1"/&gt;&lt;FUNCTION NAME="CITN" ID="534" ACCESS="1"/&gt;&lt;FUNCTION NAME="batch report" ID="553" ACCESS="1"/&gt;&lt;FUNCTION NAME="ModellerWS" ID="557" ACCESS="1"/&gt;&lt;FUNCTION NAME="Sector Analysis" ID="562" ACCESS="1"/&gt;&lt;/QUEST_FUNCTIONS&gt;&lt;ADMIN_FUNCTIONS&gt;&lt;FUNCTION NAME="administrator" ID="531" ACCESS="0"/&gt;&lt;FUNCTION NAME="author" ID="532" ACCESS="1"/&gt;&lt;FUNCTION NAME="publisher" ID="533" ACCESS="0"/&gt;&lt;FUNCTION NAME="editor" ID="539" ACCESS="0"/&gt;&lt;/ADMIN_FUNCTIONS&gt;&lt;/PERMISSIONS&gt;&lt;/ROOT&gt; 
10-04 14:30:08.696: DEBUG/login result is(439): </LoginResult></LoginResponse></soap:Body></soap:Envelope> 

子节点来了这样USER_ID =“testpub2”我必须转换xnode和获得的价值如何隐藏的XML节点?并使用saxparser获取价值。我可以直接取值吗?

+0

你的意思是写这个字符串到一个XML文件使用Java? – Nivas 2010-10-08 06:10:08

+0

嗯,“我怎么写一个字符串到Java文件”是不是很有趣。那么你如何只写SOAP体的内容( XML文件?) – 2010-10-08 06:15:34

回答

32

下面的代码将字符串转换为XML文档。一旦你有文件,你可以浏览节点,或者你可以写入一个文件等。

import java.io.*; 
import javax.xml.parsers.*; 
import org.w3c.dom.*; 
import org.xml.sax.*; 

public class Util { 

    public static Document stringToDom(String xmlSource) 
      throws SAXException, ParserConfigurationException, IOException { 
     DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); 
     DocumentBuilder builder = factory.newDocumentBuilder(); 
     return builder.parse(new InputSource(new StringReader(xmlSource))); 
    } 
} 
+0

我的孩子节点来了这样USER_ID =“testpub2”我必须转换XML格式,并采取值我该怎么办? – mohan 2010-10-08 08:11:57

0
public void writeFile(String yourXML){ 
    BufferedWriter out = new BufferedWriter(new FileWriter("outfilename.xml")); 
    try { 
     out.write(yourXML); 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } finally { 
     out.close(); 
    } 
} 
+0

不应该那个“aString”是你的XML? – Hobo 2010-10-08 06:26:32

+2

只是给你一些我学到的东西的反馈。不要在try块中调用close方法,如果抛出异常,最终得到一个打开的文件,把它放到你的finally块中,因为即使抛出异常,也会调用finally块 – 2010-10-08 08:18:18

+0

我的节点格式不正确USER_ID = “testpub2”我想转换xml格式并取值 – mohan 2010-10-08 08:20:22

相关问题