XML解析与Child不值解析 [英] XML parsing with Child not value parsing
本文介绍了XML解析与Child不值解析的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
XML解析与Child not value parsing
import java.io.File;
import java.io.FileInputStream;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathFactory;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;
import com.sun.org.apache.xml.internal.dtm.ref.DTMNodeList;
public class XPathEvaluator {
/ *
* ServiceGroup serviceGroup = new ServiceGroup();列表与LT;服务>
* requiredServices = new ArrayList< Service>();列表与LT;服务>
* recommendationsandedServices = new ArrayList< Service>(); Service service = new
* Service();
* /
public void evaluateDocument(File xmlDocument){
try {
XPathFactory factory = XPathFactory.newInstance();
XPath xPath = factory.newXPath();
String requiredServicesExpression =/ Envelope / Header;
InputSource requiredServicesInputSource = new InputSource(
new FileInputStream(xmlDocument));
DTMNodeList requiredServicesNodes =(DTMNodeList)xPath.evaluate(
requiredServicesExpression,requiredServicesInputSource,
XPathConstants.NODESET);
System.out.println(requiredServicesNodes.getLength());
NodeList requiredNodeList =(NodeList)requiredServicesNodes; (int i = 0; i< requiredNodeList.getLength(); i ++){
Node node = requiredNodeList.item(i);
System.out.println(node.getChildNodes());
}
} catch(异常e){
e.printStackTrace();
}
}
public static void main(String [] argv){
XPathEvaluator evaluateator = new XPathEvaluator();
文件xmlDocument = new File(d://eva.xml);
evaluateator.evaluateDocument(xmlDocument);
}
}
我的xml是以下是我尝试解析标题信息
<?xml version =1.0encoding =UTF-8 ?>
< Envelope>
<标题>
< User id =MAKRISH/>
< Request-Id id =1/>
< Type name =Response/>
< Application-Source name =vssversion =1.0/>
< Application-Destination name =test/>
< Outgo-Timestamp date =2012-08-24time =14:50:00/>
< DealerCode> 08301< / DealerCode>
< Market> 00000< / Market>
< / Header>
< / Envelope>
我无法获取Header小孩如何获取它在getchildNodes方法中给我null 。
解决方案
这个相关的问题有一个很好的例子,用xpath解析xml。
我已经调试了你的代码,而getChildNodes调用实际上并不返回null,但它有一个令人困惑的 toString()
。 p>
XML parsing with Child not value parsing
import java.io.File;
import java.io.FileInputStream;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathFactory;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;
import com.sun.org.apache.xml.internal.dtm.ref.DTMNodeList;
public class XPathEvaluator {
/*
* ServiceGroup serviceGroup = new ServiceGroup(); List<Service>
* requiredServices = new ArrayList<Service>(); List<Service>
* recommandedServices = new ArrayList<Service>(); Service service = new
* Service();
*/
public void evaluateDocument(File xmlDocument) {
try {
XPathFactory factory = XPathFactory.newInstance();
XPath xPath = factory.newXPath();
String requiredServicesExpression = "/Envelope/Header";
InputSource requiredServicesInputSource = new InputSource(
new FileInputStream(xmlDocument));
DTMNodeList requiredServicesNodes = (DTMNodeList) xPath.evaluate(
requiredServicesExpression, requiredServicesInputSource,
XPathConstants.NODESET);
System.out.println(requiredServicesNodes.getLength());
NodeList requiredNodeList = (NodeList) requiredServicesNodes;
for (int i = 0; i < requiredNodeList.getLength(); i++) {
Node node = requiredNodeList.item(i);
System.out.println(node.getChildNodes());
}
} catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String[] argv) {
XPathEvaluator evaluator = new XPathEvaluator();
File xmlDocument = new File("d://eva.xml");
evaluator.evaluateDocument(xmlDocument);
}
}
my xml is following in this i am try to parse header information
<?xml version="1.0" encoding="UTF-8"?>
<Envelope>
<Header>
<User id="MAKRISH"/>
<Request-Id id="1"/>
<Type name="Response"/>
<Application-Source name="vss" version="1.0"/>
<Application-Destination name="test" />
<Outgo-Timestamp date="2012-08-24" time="14:50:00"/>
<DealerCode>08301</DealerCode>
<Market>00000</Market>
</Header>
</Envelope>
i am not able to get Header child how can i get them it is giving me null on getchildNodes method. i have check for many solution but get any thing.
解决方案
The accepted answer to this related question has a good example of parsing xml using xpath.
I've debugged into your code, and the getChildNodes call is in fact not returning null, but it has got a confusing toString()
.
这篇关于XML解析与Child不值解析的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文