I am trying to read the whole XML file in Java. Below is my XML file-
<?xml version="1.0" encoding="UTF-8"?>
<app hash='nv', name='Tech', package = '1.0', version='13', filesize='200', create_date='01-03-1987', upate_date='07-09-2013' >
<url>
<name>RJ</name>
<score>10</score>
</url>
<url>
<name>ABC</name>
<score>20</score>
</url>
</app>
And below is my code, I am using to read the full XML file as shown above and then get hash, name, package etc value from that XML file.
public static void main(String[] args) {
try {
File fXmlFile = new File("C:\\ResourceFile\\app.xml");
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
Document doc = dBuilder.parse(fXmlFile);
System.out.println(doc);
} catch (Exception e) {
}
}
And as soon as I am running the above program. I am always getting the below excpetion-
[Fatal Error] app.xml:2:22: Element type "app" must be followed by either attribute specifications, ">" or "/>".
Any idea why it is happening?
If you don't want to parse it as XML and only to show as a String maybe you want to use
a BufferedReader and readLine() store it in a StringBuilder and then show it. How to read a file
Example:
public String readFile(String path) throws IOException{
StringBuilder sb = new StringBuilder();
try (BufferedReader br = new BufferedReader(new FileReader(path))){
while ((String sCurrentLine = br.readLine()) != null) {
sb.append(sCurrentLine);
}
}
return sb.toString();
}
EDIT In java 8 you can just simply use
String xml = Files.lines(Paths.getPath(path)).collect(Collectors.joining("\n"));
There is syntax error in your xml. The attributes of the element should not be separated by a comma. It should be like,
<?xml version="1.0" encoding="UTF-8"?>
<app hash='nv' name='Tech' package='1.0' version='13' filesize='200' create_date='01-03-1987' upate_date='07-09-2013' >
<url>
<name>RJ</name>
<score>10</score>
</url>
<url>
<name>ABC</name>
<score>20</score>
</url>
</app>
try{
InputStream is = getAssets().open("HeadWork_JackWell.xml");
DocumentBuilderFactory dFactory= DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder= dFactory.newDocumentBuilder();
Document doc= dBuilder.parse(is);
try {
StringWriter sw = new StringWriter();
TransformerFactory tf = TransformerFactory.newInstance();
Transformer transformer = tf.newTransformer();
transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "no");
transformer.setOutputProperty(OutputKeys.METHOD, "xml");
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
transformer.transform(new DOMSource(doc), new StreamResult(sw));
String s=sw.toString();
System.out.println(s);
} catch (Exception ex) {
throw new RuntimeException("Error converting to String", ex);
}
Related
When splitting a huge XML file I saw a very nice solution using Stax and Transformer.transform(). Nice BUT I see that some tags got lost. Why is that?
An XML file with Name... gives the following result. In the EVENT occasions the element tag is ommited.
Element: <?xml version="1.0" encoding="UTF-8"?><car><name>car1</name></car>
Element: <?xml version="1.0" encoding="UTF-8"?><name>car2</name>
Element: <?xml version="1.0" encoding="UTF-8"?><car><name>car3</name></car>
Element: <?xml version="1.0" encoding="UTF-8"?><name>car4</name>
How can I get the right elements? Has this to do with that transform( s, r) interferes with the input stream reading?
This is my code (which I saw in many places like this one). There is no change when using a StringReader or a FileReader.
I expected this: loop { advance to start-tag; get access to that element }
What I see is: 1st: the element + 2nd: parts of the element + repeated.
String testCars = "<root><car><name>car1</name></car><car><name>car2</name></car><car><name>car3</name></car><car><name>car4</name></car></root>";
String element = "car";
try {
XMLInputFactory factory = XMLInputFactory.newInstance();
XMLStreamReader streamReader = factory.createXMLStreamReader(new StringReader(testCars));
streamReader.nextTag();
TransformerFactory tf = TransformerFactory.newInstance();
Transformer t = tf.newTransformer();
while(streamReader.nextTag() == XMLStreamConstants.START_ELEMENT) {
StringWriter writer = new StringWriter();
StreamResult result = new StreamResult(writer);
t.transform(new StAXSource(streamReader), result);
System.out.println("Element: " + writer.toString());
}
} catch (Exception e) { ... }
Thanks to Andreas, this is the solution:
String testCars = "<root><car><name>car1</name></car><other><something>Unknown</something></other><car><name>car2</name></car></root>";
XMLInputFactory factory = XMLInputFactory.newInstance();
try {
XMLStreamReader streamReader = factory.createXMLStreamReader(new StringReader(testCars));
streamReader.nextTag();
TransformerFactory tf = TransformerFactory.newInstance();
Transformer t = tf.newTransformer();
streamReader.nextTag();
while ( streamReader.isStartElement() ||
( ! streamReader.hasNext() && streamReader.nextTag() == XMLStreamConstants.START_ELEMENT)) {
StringWriter writer = new StringWriter();
StreamResult result = new StreamResult(writer);
t.transform(new StAXSource(streamReader), result);
System.out.println( "XmlElement: " + writer.toString());
}
} catch (Exception e) { ... }
Input is:
<root>
<car>
<name>car1</name>
</car>
<other>
<something>Unknown</something>
</other>
<car>
<name>car2</name>
</car>
</root>
Output is:
XmlElement: <?xml version="1.0" encoding="UTF-8"?><car><name>car1</name></car>
XmlElement: <?xml version="1.0" encoding="UTF-8"?><other><something>Unknown</something></other>
XmlElement: <?xml version="1.0" encoding="UTF-8"?><car><name>car2</name></car>
I am making a web application, and I need to make a log. I am storing this log as an xml file.
These are my paths:
log.xml -> org.codealizer.quizme.resources
Servlet -> org.codealizer.quizme.servlets
When my application runs, it gives me a success result, but the xml file is not modified.
This is my code to modify the xml file:
private void createLog(HttpServletRequest request,
HttpServletResponse response, PrintWriter out) {
String ipAddress = request.getRemoteAddr();
int port = request.getRemotePort();
String clientInformation = request.getHeader("user-agent");
try {
DocumentBuilderFactory factory = DocumentBuilderFactory
.newInstance();
DocumentBuilder dBuilder = factory.newDocumentBuilder();
Document doc = dBuilder.parse(getClass().getResourceAsStream(
"/org/codealizer/quizme/resources/log.xml"));
doc.getDocumentElement().normalize();
Node log = doc.getFirstChild();
Element item = doc.createElement("item");
Element time = doc.createElement("time");
time.appendChild(doc.createTextNode("" + System.currentTimeMillis()));
item.appendChild(time);
Element access = doc.createElement("access");
access.appendChild(doc.createTextNode(getClass().getName()));
item.appendChild(access);
Element address = doc.createElement("address");
address.appendChild(doc.createTextNode(ipAddress + ":" + port));
item.appendChild(address);
Element client = doc.createElement("client");
client.appendChild(doc.createTextNode(clientInformation));
item.appendChild(client);
log.appendChild(item);
// Update
TransformerFactory transformerFactory = TransformerFactory
.newInstance();
Transformer transformer = transformerFactory.newTransformer();
DOMSource source = new DOMSource(doc);
StreamResult result = new StreamResult(getClass().getResource(
"/org/codealizer/quizme/resources/log.xml").getFile());
// Output to console for testing
// StreamResult result = new StreamResult(System.out);
transformer.transform(source, result);
} catch (SAXException | IOException | ParserConfigurationException
| TransformerException e) {
out.println(e.getMessage());
}
This is my current log.xml file
<?xml version="1.0" encoding="UTF-8"?>
<log></log>
Could someone please help me fix this issue? Thx a lot :)
I tried to convert xml output to string my code is below:
OAuthRequest request2 = new OAuthRequest(Verb.GET,"https://api.linkedin.com/v1/people/~:(first-name,last-name,email-address)");
request2.addHeader("Authorization", "Bearer "+accesstok);
Response response2 = request2.send();
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
Document doc = builder.parse(response2.getBody());
StringWriter stringWriter = new StringWriter();
Transformer transformer = TransformerFactory.newInstance().newTransformer();
transformer.transform(new DOMSource(doc), new StreamResult(stringWriter));
String strFileContent = stringWriter.toString(); //This is string data of xml file
System.out.println(strFileContent);
i am getting the output as below:
java.net.MalformedURLException: no protocol: <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<person>
<first-name>xxxxxxx</first-name>
<last-name>xxxx</last-name>
<email-address>xxxxxxxxxx</email-address>
</person>
how do i extract first-name
I'm using the org.w3c.dom package to parse the gml schemas (http://schemas.opengis.net/gml/3.1.0/base/).
When I parse the gmlBase.xsd schema and then save it back out, the quote characters around GeometryCollections in the BagType complex type come out converted to bad characters (See code below).
Is there something wrong with how I'm parsing or saving the xml, or is there something in the schema that is off?
Thanks,
Curtis
public static void main(String[] args) throws IOException
{
File schemaFile = File.createTempFile("gml_", ".xsd");
FileUtils.writeStringToFile(schemaFile, getSchema(new URL("http://schemas.opengis.net/gml/3.1.0/base/gmlBase.xsd")));
System.out.println("wrote file: " + schemaFile.getAbsolutePath());
}
public static String getSchema(URL schemaURL)
{
try
{
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
Document doc = db.parse(new InputSource(new StringReader(IOUtils.toString(schemaURL.openStream()))));
Element rootElem = doc.getDocumentElement();
rootElem.normalize();
TransformerFactory tFactory = TransformerFactory.newInstance();
Transformer transformer = tFactory.newTransformer();
DOMSource source = new DOMSource(doc);
ByteArrayOutputStream xmlOutStream = new ByteArrayOutputStream();
StreamResult result = new StreamResult(xmlOutStream);
transformer.transform(source, result);
return xmlOutStream.toString();
}
catch (Exception e)
{
e.printStackTrace();
}
return "";
}
I'm suspicious of this line:
Document doc = db.parse(new InputSource(
new StringReader(IOUtils.toString(schemaURL.openStream()))));
I don't know what IOUtils.toString does here but presumably it's assuming a particular encoding, without taking account of the XML declaration.
Why not just use:
Document doc = db.parse(schemaURL.openStream());
Likewise your FileUtils.writeStringToFile doesn't appear to specify a character encoding... which encoding does it use, and why encoding is in the StreamResult?
I have a java class that applies an xslt to all xml files in a directory and performs a transformation on every xml it finds and prints out the complete filename.
My question is how would I create an xml (Files.xml),which would have the following format, and then ouputs the file name, file type and file extension to Files.xml?
<files>
<file>
<name> ThisFile </name>
<type> xml </type>
<extension> .xml </extension>
</file>
<file>
<name> AnotherFile </name>
<type> xml </type>
<extension> .xml </extension>
</file>
etc....
</files>
Once again I appreciate any and all help I get!
I would recommend using a serialization utility like JAXB or XStream to serialize the file model directly but I leave you here a small sample that builds the document from scratch.
public void serializeXmlFiles(ArrayList<File> files) throws ParserConfigurationException, TransformerException {
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
Document doc = db.newDocument();
Element filesElement = doc.createElement("files");
doc.appendChild(filesElement);
for (File file : files) {
Element fileElement = doc.createElement("file");
Element nameElement = doc.createElement("name");
nameElement.setTextContent(file.getName());
Element typeElement = doc.createElement("type");
typeElement.setTextContent("xml");
Element extElement = doc.createElement("extension");
extElement.setTextContent(".xml");
fileElement.appendChild(nameElement);
fileElement.appendChild(typeElement);
fileElement.appendChild(extElement);
filesElement.appendChild(fileElement);
}
saveXMLDocument("files.xml", doc);
}
public boolean saveXMLDocument(String fileName, Document doc) throws TransformerException {
File xmlOutputFile = new File(fileName);
FileOutputStream fos;
Transformer transformer;
try {
fos = new FileOutputStream(xmlOutputFile);
} catch (FileNotFoundException e) {
return false;
}
TransformerFactory transformerFactory = TransformerFactory.newInstance();
transformer = transformerFactory.newTransformer();
DOMSource source = new DOMSource(doc);
StreamResult result = new StreamResult(fos);
transformer.transform(source, result);
return true;
}
Hope it helps.