在Java中使用XPath解析XML
我想解析此XML文件:在Java中使用XPath解析XML,java,xml,xpath,Java,Xml,Xpath,我想解析此XML文件: <NameDefinitions xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../Documents/Calcs/FriendlyNames.xsd"> <NameDefinition> <ID>/Temporary/EIC/EICWorksheetPP/WagesLessT
<NameDefinitions xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="../Documents/Calcs/FriendlyNames.xsd">
<NameDefinition>
<ID>/Temporary/EIC/EICWorksheetPP/WagesLessThanAdjustments</ID>
<Name>you have more income that doesn't count than income that counts</Name>
<BooleanPhrases>
<True><Text>you have more income that doesn't count than income that counts</Text></True>
<False><Text>you have more income that counts than income that doesn't count</Text></False>
<Blank><Text>we don't if you have more income that doesn't count than income that counts</Text></Blank>
</BooleanPhrases>
<BooleanQuestions>
<True><Text>Why do I have more income that doesn't count than income that counts?</Text></True>
<False><Text>Why do I have more income that counts than income that doesn't count?</Text></False>
<Blank><Text>Why don't you know if I have more income that doesn't count than income that counts?</Text></Blank>
</BooleanQuestions>
</NameDefinition>
<NameDefinition>
<ID>/Temporary/EIC/HaveInaccurateInfo</ID>
<Name>you told us your info is incorrect</Name>
<BooleanPhrases>
<True><Text>you told us some of your info is incorrect</Text></True>
<False><Text>you told us your info is correct</Text></False>
<Blank><Text>we don't know whether your info is correct</Text></Blank>
</BooleanPhrases>
<BooleanQuestions>
<True><Text>Why is some of my info incorrect?</Text></True>
<False><Text>Why is my info correct?</Text></False>
<Blank><Text>Why don't you know if my info is correct?</Text></Blank>
</BooleanQuestions>
</NameDefinition>
</NameDefinitions>
例外情况:
java.io.IOException: Stream closed
at java.io.BufferedInputStream.getBufIfOpen(BufferedInputStream.java:162)
at java.io.BufferedInputStream.fill(BufferedInputStream.java:206)
at java.io.BufferedInputStream.read(BufferedInputStream.java:254)
javax.xml.xpath.XPathExpressionException: java.io.IOException: Stream closed
at org.apache.xpath.jaxp.XPathImpl.evaluate(XPathImpl.java:481)
Caused by: java.io.IOException: Stream closed
at java.io.BufferedInputStream.getBufIfOpen(BufferedInputStream.java:162)
at java.io.BufferedInputStream.fill(BufferedInputStream.java:206)
at java.io.BufferedInputStream.read(BufferedInputStream.java:254)
我已经关闭了溪流,但显然还是个问题 //将xml-api.jar用于下面的类,如DocumentBuilderFactory
//XPATH expressions
public static final String IdExpr = "/NameDefinition/@Id";
public static final String NameExpr = "/NameDefinition/@Name";
Map<String,String> evaluateXML(String file, String IdExpr, String NameExpr) {
Map < String,
String > returnMap = null;
try {
FileInputStream fis = new FileInputStream(new File(file));
DocumentBuilderFactory builderFactory = DocumentBuilderFactory
.newInstance();
DocumentBuilder builder = builderFactory.newDocumentBuilder();
Document xmlDocument = builder.parse(fis);
XPath xPath = XPathFactory.newInstance().newXPath();
String Id = xPath.compile(IdExpr).evaluate(
xmlDocument);
String Name = xPath.compile(NameExpr).evaluate(
xmlDocument);
returnMap = new HashMap < String,
String > ();
returnMap.put(Id, Name);
}
//XPATH表达式
公共静态最终字符串IdExpr=“/namefinition/@Id”;
公共静态最终字符串NameExpr=“/NameDefinition/@Name”;
映射evaluateXML(字符串文件、字符串IdExpr、字符串名称EXPR){
MapreturnMap=null;
试一试{
FileInputStream fis=新FileInputStream(新文件(文件));
DocumentBuilderFactory builderFactory=DocumentBuilderFactory
.newInstance();
DocumentBuilder=builderFactory.newDocumentBuilder();
文档xmlDocument=builder.parse(fis);
XPath=XPathFactory.newInstance().newXPath();
String Id=xPath.compile(IdExpr.evaluate)(
xml文档);
String Name=xPath.compile(NameExpr.evaluate)(
xml文档);
returnMap=newhashmap();
returnMap.put(Id,Name);
}
解决方案已经提出,在发布此问题之前,我应该做一些研究:
static Map<String,String> parseDoc(String pathToFriendlyNamesFile)
throws ParserConfigurationException,
SAXException,
IOException {
File f = new File(pathToFriendlyNamesFile);
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
Document doc = db.parse(f);
Map<String,String> map = new LinkedHashMap<String, String>();
doc.getDocumentElement().normalize();
System.out.println("Root Element: "+doc.getDocumentElement().getNodeName());
NodeList nList = doc.getElementsByTagName("NameDefinition");
System.out.println("How many nodes? "+nList.getLength());
for(int i=0;i<nList.getLength();i++) {
Node nNode = nList.item(i);
if(nNode.getNodeType() == Node.ELEMENT_NODE) {
Element eElement = (Element) nNode;
String nodeID = eElement.getElementsByTagName("ID").item(0).getTextContent();
String name = eElement.getElementsByTagName("Name").item(0).getTextContent();
System.out.println("ID: "+nodeID+" Name: "+name);
map.put(nodeID, name);
}
}
return map;
}
静态映射parseDoc(字符串pathToFriendlyNamesFile)
抛出ParserConfiguration异常,
除此之外,
IOException{
文件f=新文件(pathToFriendlyNamesFile);
DocumentBuilderFactory dbf=DocumentBuilderFactory.newInstance();
DocumentBuilder db=dbf.newDocumentBuilder();
文档doc=db.parse(f);
Map Map=newlinkedhashmap();
doc.getDocumentElement().normalize();
System.out.println(“根元素:+doc.getDocumentElement().getNodeName());
NodeList nList=doc.getElementsByTagName(“名称定义”);
System.out.println(“有多少个节点?”+nList.getLength());
对于(int i=0;iIs)您必须使用x-path?如果不是,那么您可以使用DOM对象进行解析。这可能会对您有所帮助。我想使用Xpath,这不是必需的使用上述方法…这将非常简单。很简单,但由于某些原因,此实现无法工作
static Map<String,String> parseDoc(String pathToFriendlyNamesFile)
throws ParserConfigurationException,
SAXException,
IOException {
File f = new File(pathToFriendlyNamesFile);
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
Document doc = db.parse(f);
Map<String,String> map = new LinkedHashMap<String, String>();
doc.getDocumentElement().normalize();
System.out.println("Root Element: "+doc.getDocumentElement().getNodeName());
NodeList nList = doc.getElementsByTagName("NameDefinition");
System.out.println("How many nodes? "+nList.getLength());
for(int i=0;i<nList.getLength();i++) {
Node nNode = nList.item(i);
if(nNode.getNodeType() == Node.ELEMENT_NODE) {
Element eElement = (Element) nNode;
String nodeID = eElement.getElementsByTagName("ID").item(0).getTextContent();
String name = eElement.getElementsByTagName("Name").item(0).getTextContent();
System.out.println("ID: "+nodeID+" Name: "+name);
map.put(nodeID, name);
}
}
return map;
}