我需要从字符串缓冲区或字符串解析xml数据..i代码如下.这里就行了document doc =db.parse(eventXml )......抛出异常 - plesae找到下面的代码和异常.请帮助我
码
eventXml = strBuffer.toString();
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
Document doc = db.parse(eventXml );
Run Code Online (Sandbox Code Playgroud)
例外
java.net.MalformedURLException: no protocol: <?xml version="1.0" encoding="UTF-8
" standalone="yes"?>%0A<EventInfo xmlns="http://www.telenet.be/oms/event">%0A
<TelenetEventInfo className="be.telenet.oms.events.OmsAsapJsrpNotifyEvent">%0A
<SimpleFields>%0A <SimpleField>%0A <FieldName>C
ompletion_Date_Time</FieldName>%0A <FieldValue>04/08/2009 08:34:0
1</FieldValue>%0A </SimpleField>%0A <SimpleField>%0A
<FieldName>Originator</FieldName>%0A <FieldValue>System
</FieldValue>%0A </SimpleField>%0A <SimpleField>%0A
<FieldName>Status</FieldName>%0A <FieldValue>S</FieldVal
ue>%0A </SimpleField>%0A <SimpleField>%0A <
FieldName>Workorder_ID</FieldName>%0A <FieldValue>I00054132231-09
8</FieldValue>%0A </SimpleField>%0A </SimpleFields>%0A
<ArrayData>%0A <ArrayNames>%0A <ArrayName>Parameters</
ArrayName>%0A <ArrayFieldEntry>%0A <ArraySubFi
eld>[0].Parameter_Name</ArraySubField>%0A <ArraySubFieldValue
>WARNING_TEXT</ArraySubFieldValue>%0A </ArrayFieldEntry>%0A
<ArrayFieldEntry>%0A <ArraySubField>[0].Parameter_v
alue</ArraySubField>%0A <ArraySubFieldValue>UnknownKeyExcepti
on-Retrieving a webURL%0A for …Run Code Online (Sandbox Code Playgroud) 解析下面的XML时,首先url-malformed-exception解析代码而不是给出xml字符串我使用了这段代码
Document doc=dBuilder.parse(newInputSource(newByteArrayInputStream(xmlResponse.getBytes("utf-8"))));
Run Code Online (Sandbox Code Playgroud)
根据这个链接
java.net.MalformedURLException:没有协议
现在我得到的节点值为null.我怎样才能克服这一点.在for循环的代码中,我已经提到过节点的空值即将来临
我使用以下代码:
try {
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
Document doc = dBuilder.parse(new InputSource(new ByteArrayInputStream(xmlResponse.getBytes("utf-8"))));
//read this - https://stackoverflow.com/questions/13786607/normalization-in-dom-parsing-with-java-how-does-it-work
doc.getDocumentElement().normalize();
System.out.println("Root element :" + doc.getDocumentElement().getNodeName());
XPath xPath = XPathFactory.newInstance().newXPath()
String expression = "/GetMatchingProductForIdResponse/GetMatchingProductForIdResult/Products/Product"
System.out.println(expression)
NodeList nodeList = (NodeList) xPath.compile(expression).evaluate(doc, XPathConstants.NODESET)
System.out.println("the size will be of the node list ${nodeList.getLength()}");
for (int i = 0; i < nodeList.getLength(); i++) {
System.out.println(nodeList.item(i).getNodeValue()+"the value coming will be "); // here i am …Run Code Online (Sandbox Code Playgroud) 我尝试了所有不同类型的组合,但我只是没有得到它想要的:
java.net.MalformedURLException: no protocol: <?xml version="1.0" encoding="UTF-8" standalone="yes"?><ds><ds>...
at java.net.URL.<init>(URL.java:593)
at java.net.URL.<init>(URL.java:490)
at java.net.URL.<init>(URL.java:439)
at com.sun.org.apache.xerces.internal.impl.XMLEntityManager.setupCurrentEntity(XMLEntityManager.java:620)
at com.sun.org.apache.xerces.internal.impl.XMLVersionDetector.determineDocVersion(XMLVersionDetector.java:148)
at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:805)
at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:770)
at com.sun.org.apache.xerces.internal.parsers.XMLParser.parse(XMLParser.java:141)
at com.sun.org.apache.xerces.internal.parsers.DOMParser.parse(DOMParser.java:243)
at com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderImpl.parse(DocumentBuilderImpl.java:339)
at javax.xml.parsers.DocumentBuilder.parse(DocumentBuilder.java:177)
Run Code Online (Sandbox Code Playgroud)
我查看了其他问题,但我不明白我的 XML 有什么问题。
这是完整的 XML(删除了一些纯文本):
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<ds>
<ds>
<cue>ABGB : §§ 786 , 810 , 812 </cue>Die Kosten ... <cue>Anmerkung : </cue>
... <cue>Bestätigung von </cue>7
<Relation bewertung="1">Ob 56/10a </Relation>= Zak
<Relation bewertung="1">2010/773 , 440 </Relation>.
</ds>
</ds>
Run Code Online (Sandbox Code Playgroud)
生产代码:
DocumentBuilderFactory icFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder icBuilder;
Document parse …Run Code Online (Sandbox Code Playgroud)