使用XPath进行XML解析时,找不到文件异常

时间:2015-11-05 10:02:56

标签: java xml xpath filenotfoundexception

我正在尝试编写一个Java程序来将属性添加到现有的XML文件中.XML是:

 <Items  ApplyDifferences="Y" ValidateItems="Y" CompleteInventoryFlag="Y"  ShipNode=" ACMEUSDC" >
        <Item InventoryOrganizationCode="ACMEUS" ItemID="2000033672234"  
        ProductClass="GOOD" UnitOfMeasure="EACH">
           <Supplies>
<Supply AvailabilityType="TRACK" Quantity="20.00" ShipByDate="2500-01-01" 
    SupplyType="ONHAND"/> 
         </Supplies>
        </Item>
        <Item InventoryOrganizationCode="ACMEUS" ItemID="2000033672235"  
        ProductClass="GOOD" UnitOfMeasure="EACH">
           <Supplies>
  <Supply AvailabilityType="TRACK" Quantity="25.00" ShipByDate="2500-01-01"   
    SupplyType="ONHAND"/> 
           </Supplies>
       </Item>
       <Item InventoryOrganizationCode="ACMEUS" ItemID="2000033672236" 
        ProductClass="GOOD" UnitOfMeasure="EACH">
          <Supplies>
    <Supply AvailabilityType="TRACK" Quantity="25.00" ShipByDate="2500-01-01"  
    SupplyType="ONHAND"/> 
         </Supplies>
       </Item>
 </Items>

我的Java代码是:

    package xmltest;
import java.io.File;
import java.io.IOException;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathExpressionException;
import javax.xml.xpath.XPathFactory;

import org.apache.xpath.XPathAPI;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;


public class Testxml4 {

    public static void main(String[] args) throws ParserConfigurationException, SAXException, IOException, XPathExpressionException, TransformerException {
        // TODO Auto-generated method stub

        DocumentBuilderFactory factory =DocumentBuilderFactory.newInstance();
        DocumentBuilder builder = factory.newDocumentBuilder();

        File inputFile = new File("C:/Users/praveen.sharma/Desktop/XMLs/xml4.xml");
        System.out.println(new File(".").getAbsolutePath());
        System.out.println(inputFile.exists());


        Document doc = builder.parse(inputFile);
        Element element = (Element) XPathAPI.selectSingleNode(doc,"Order/OrderLines/OrderLine[@PrimeLineNo='6']/OrderLineSourcingControls/OrderLineSourcingCntrl[@Node='Node1']");
        element.setAttribute("Node", "Node02");

        TransformerFactory transformerFactory = TransformerFactory.newInstance();
        Transformer transformer = transformerFactory.newTransformer();
        DOMSource source = new DOMSource(doc);
        StreamResult result = new StreamResult(inputFile);
        transformer.transform(source, result);

    }

}

这是我得到的错误:

Exception in thread "main" javax.xml.transform.TransformerException: java.io.FileNotFoundException: file:\C:\Users\praveen.sharma\Desktop\XMLs\xml4.xml (The filename, directory name, or volume label syntax is incorrect)
    at org.apache.xalan.transformer.TransformerIdentityImpl.createResultContentHandler(TransformerIdentityImpl.java:263)
    at org.apache.xalan.transformer.TransformerIdentityImpl.transform(TransformerIdentityImpl.java:296)
    at xmltest.Testxml4.main(Testxml4.java:46)
Caused by: java.io.FileNotFoundException: file:\C:\Users\praveen.sharma\Desktop\XMLs\xml4.xml (The filename, directory name, or volume label syntax is incorrect)
    at java.io.FileOutputStream.open(Native Method)
    at java.io.FileOutputStream.<init>(Unknown Source)
    at java.io.FileOutputStream.<init>(Unknown Source)
    at org.apache.xalan.transformer.TransformerIdentityImpl.createResultContentHandler(TransformerIdentityImpl.java:253)
    ... 2 more
---------
java.io.FileNotFoundException: file:\C:\Users\praveen.sharma\Desktop\XMLs\xml4.xml (The filename, directory name, or volume label syntax is incorrect)
    at java.io.FileOutputStream.open(Native Method)
    at java.io.FileOutputStream.<init>(Unknown Source)
    at java.io.FileOutputStream.<init>(Unknown Source)
    at org.apache.xalan.transformer.TransformerIdentityImpl.createResultContentHandler(TransformerIdentityImpl.java:253)
    at org.apache.xalan.transformer.TransformerIdentityImpl.transform(TransformerIdentityImpl.java:296)
    at xmltest.Testxml4.main(Testxml4.java:46)

我可以向您保证,我的XML存在于指定路径并且具有所有必需的权限。

1 个答案:

答案 0 :(得分:1)

您正在尝试将输出写入您正在阅读的文件。你应该写一个不同的文件。

如果你想&#34;改变&#34;同一个文件,最安全的方法是写入临时文件,然后关闭所有流,然后删除原始文件,然后将临时文件重命名为原始名称。