0

I am trying to write a Java program to add attribute to an already existing XML file.The XML is :

 <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>

My Java code is:

    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);

    }

}

This is the error I am getting:

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)

I can assure you my XML is present at the specified path and has all required permission.

4
  • you successfully read and parse the file. It is the output which fails. Could be because the file is still opened and locked by another program. You could first try to write to a different file and see if that succeeds. Commented Nov 5, 2015 at 10:14
  • Tried creating another output file.Got the file not found error for output file. Commented Nov 5, 2015 at 10:15
  • then you probably have a permission problem Commented Nov 5, 2015 at 10:17
  • Till yesterday it was working fine.Hell I even have the generated output file.I don't know what happened but I am getting the error since morning. Commented Nov 5, 2015 at 10:18

1 Answer 1

1

You are trying to write the output to the same file from where you are reading. You should write to a different file instead.

If you want to "change" the same file, then the safest way is to write to a temporary file, then close all streams, then delete the original file and then rename the temporary file to the original name.

Sign up to request clarification or add additional context in comments.

1 Comment

As I mentioned in previous comments,I tried creating a different output file.As a matter of fact, initially I was doing that only.I switched to editing original file only once i start getting FileNotFound Exception.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.