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.