从inpustreamjava从xml获取信息
我有一个从inputstream读取xml的程序,这是inputstream的结果xml从inpustreamjava从xml获取信息,java,xml,inputstream,Java,Xml,Inputstream,我有一个从inputstream读取xml的程序,这是inputstream的结果xml <?xml version="1.0" encoding="utf-8"?> <soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/s
<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Body>
<GetRouteSummaryForStopResponse xmlns="http://octranspo.com">
<GetRouteSummaryForStopResult>
<StopNo xmlns="http://tempuri.org/">string</StopNo>
<StopDescription
xmlns="http://tempuri.org/">string</StopDescription>
<Error xmlns="http://tempuri.org/">string</Error>
<Routes xmlns="http://tempuri.org/">
<Route>
<RouteNo>int</RouteNo>
<DirectionID>int</DirectionID>
<Direction>string</Direction>
<RouteHeading>string</RouteHeading>
</Route>
<Route>
<RouteNo>int</RouteNo>
<DirectionID>int</DirectionID>
<Direction>THIS IS A STRING</Direction>
<RouteHeading>string</RouteHeading>
</Route>
</Routes>
</GetRouteSummaryForStopResult>
</GetRouteSummaryForStopResponse>
</soap:Body>
</soap:Envelope>
这是解析xml并连接到主机的代码
\uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu编辑:
public InputStream HTTPrequest(String stopNo){
String parameterURL = "appID="+this.appId+"&apiKey="+this.AppKey+"&routeNo=1&stopNo="+stopNo+"";
URL url;
HttpURLConnection connection = null;
try {
url = new URL(this.requestGetRouteSummaryForStop);
connection = (HttpURLConnection) url.openConnection();
connection.setDoOutput(true);
connection.setDoInput(true);
connection.setInstanceFollowRedirects(false);
connection.setRequestMethod("POST");
connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
connection.setRequestProperty("charset", "utf-8");
connection.setRequestProperty("Content-Length", "" + Integer.toString(parameterURL.getBytes().length));
connection.setUseCaches (false);
//send request
DataOutputStream wr = new DataOutputStream (
connection.getOutputStream ());
wr.writeBytes (parameterURL);
wr.flush ();
wr.close ();
//Get Response
InputStream is = connection.getInputStream();
return is;
} catch (Exception e) {
System.out.print("ERROR: ");
e.printStackTrace();
return null;
}finally{
if(connection != null){
connection.disconnect();
}
}
}
public void parseXmlData(InputStream response){
InputStream is = response;
try {
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
dbf.setNamespaceAware(false);
Document doc = dbf.newDocumentBuilder().parse(is);
XPathFactory xf = XPathFactory.newInstance();
XPath xPath = xf.newXPath();
// Find direction ANY where within the document...
XPathExpression xExp = xPath.compile("//DirectionID");
NodeList nl = (NodeList) xExp.evaluate(doc, XPathConstants.NODESET);
for (int index = 0; index < nl.getLength(); index++) {
Node node = nl.item(index);
System.out.println(node.getTextContent());
}
// Find the direction node's any where in the document
// where it's a child of Route, which has a child
// node called RouteNo with the text value of 'int'...
xExp = xPath.compile("//Route[RouteNo = 'int']/Direction");
nl = (NodeList) xExp.evaluate(doc, XPathConstants.NODESET);
for (int index = 0; index < nl.getLength(); index++) {
Node node = nl.item(index);
System.out.println(node.getTextContent());
}
} catch (Exception exp) {
exp.printStackTrace();
}
}
你有两个选择开始 你可以 将XML加载到文档对象模型中。这允许您完全访问XML以查询它(并可能修改它) 如果您需要进行多次处理,或者希望在DOM加载到内存中并且始终可用时对数据进行多次查询,那么这是很好的 如果XML文件很大就不太好了,因为DOM需要相当大的内存 DOM方法为您提供了使用支持的能力。XPath是XML的查询API(甚至可以用于结构良好的HTML) 有关更多详细信息,请参阅 你可以 使用SAX解析器在加载XML文档时对其进行处理。这提供了回调方法/事件,当解析过程中的某些内容引发它们时,这些方法/事件将被调用 如果文档很大,或者只需要对文档执行一次处理过程(例如,构建自己的文档对象表示形式),这是很好的 它不太适合多通道处理或交互式查询 请查看以了解更多详细信息 使用XPath示例更新 虽然我确信处理名称空间很容易,但对我来说,它只是一种阻碍 下面是两个XPath查询的简单示例,该查询将从您的问题中查找文档中的所有
Direction
节点
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathExpression;
import javax.xml.xpath.XPathExpressionException;
import javax.xml.xpath.XPathFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;
public class XPathTest {
public static void main(String[] args) {
InputStream is = null;
try {
is = new FileInputStream("Soap.xml");
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
dbf.setNamespaceAware(false);
Document doc = dbf.newDocumentBuilder().parse(is);
XPathFactory xf = XPathFactory.newInstance();
XPath xPath = xf.newXPath();
// Find direction ANY where within the document...
XPathExpression xExp = xPath.compile("//Direction");
NodeList nl = (NodeList) xExp.evaluate(doc, XPathConstants.NODESET);
for (int index = 0; index < nl.getLength(); index++) {
Node node = nl.item(index);
System.out.println(node.getTextContent());
}
// Find the direction node's any where in the document
// where it's a child of Route, which has a child
// node called RouteNo with the text value of 'int'...
xExp = xPath.compile("//Route[RouteNo = 'int']/Direction");
nl = (NodeList) xExp.evaluate(doc, XPathConstants.NODESET);
for (int index = 0; index < nl.getLength(); index++) {
Node node = nl.item(index);
System.out.println(node.getTextContent());
}
} catch (ParserConfigurationException | SAXException | IOException | XPathExpressionException exp) {
exp.printStackTrace();
}
}
}
使用XML解析器?或者你已经在使用一个并且有问题了?@WilliamGaul是的,我有。我将编辑我的帖子。。发布我的代码是的,我使用XPath,但我无法在“方向”上获取数据。我如何使用XPathHi madprogrammer部署名称空间?我试图将您的代码添加到我的代码中。很抱歉使用它。但是它给了我一个错误,不允许我读取xml,我想问题是从HTTPrequest()返回到INPUTSTREAM,对吗?我已经修复了它。我添加了一个InputSource并将响应添加到documentbuilder。。谢谢你的帮助。。你的代码真的很有用
[Fatal Error] :1:1: Premature end of file.
org.xml.sax.SAXParseException; lineNumber: 1; columnNumber: 1; Premature end of file.
at com.sun.org.apache.xerces.internal.parsers.DOMParser.parse(Unknown Source)
at com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderImpl.parse(Unknown Source)
at javax.xml.parsers.DocumentBuilder.parse(Unknown Source)
at OcTranspoConnect.parseXmlData(OcTranspoConnect.java:82)
at OcTranspoConnect.main(OcTranspoConnect.java:29)
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathExpression;
import javax.xml.xpath.XPathExpressionException;
import javax.xml.xpath.XPathFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;
public class XPathTest {
public static void main(String[] args) {
InputStream is = null;
try {
is = new FileInputStream("Soap.xml");
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
dbf.setNamespaceAware(false);
Document doc = dbf.newDocumentBuilder().parse(is);
XPathFactory xf = XPathFactory.newInstance();
XPath xPath = xf.newXPath();
// Find direction ANY where within the document...
XPathExpression xExp = xPath.compile("//Direction");
NodeList nl = (NodeList) xExp.evaluate(doc, XPathConstants.NODESET);
for (int index = 0; index < nl.getLength(); index++) {
Node node = nl.item(index);
System.out.println(node.getTextContent());
}
// Find the direction node's any where in the document
// where it's a child of Route, which has a child
// node called RouteNo with the text value of 'int'...
xExp = xPath.compile("//Route[RouteNo = 'int']/Direction");
nl = (NodeList) xExp.evaluate(doc, XPathConstants.NODESET);
for (int index = 0; index < nl.getLength(); index++) {
Node node = nl.item(index);
System.out.println(node.getTextContent());
}
} catch (ParserConfigurationException | SAXException | IOException | XPathExpressionException exp) {
exp.printStackTrace();
}
}
}
string
THIS IS A STRING
string
THIS IS A STRING