How to load XML file from internet into a string - java

Currently I have a java application that loads XML from a local file into a string. My code looks like this
private String xmlFile = "D:\\mylocalcomputer\\extract-2339393.xml";
String fileStr = FileUtils.readFileToString(new File(xmlFile));
How can I get the contents of the XML file if it was located on the internet, at a URL like http://mydomain.com/xml/extract-2000.xml ?

try the sax interface
private String xmlURL = "http://mydomain.com/xml/extract-2000.xml";
XMLReader reader = XMLReaderFactory.createXMLReader();
reader.setContentHandler(handler);
reader.parse(new InputSource(new URL(xmlURL).openStream()));
For more information regarding SAX check this link

Check this code:
DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
InputStream inputStream = new FileInputStream(new File("http://mydomain.com/xml/extract-2000.xml"));
org.w3c.dom.Document doc = documentBuilderFactory.newDocumentBuilder().parse(inputStream);
StringWriter stw = new StringWriter();
Transformer serializer = TransformerFactory.newInstance().newTransformer();
serializer.transform(new DOMSource(doc), new StreamResult(stw));
stw.toString();

Related

how convert string to xml for using in createUnmarshaller for convert to pojo Entity

I want to convert a string to xml and then use the createUnmarshaller method to convert to pojo entity.
xmlString:
<File>
<code>63213713</code>
<name>textsss</name>
<player>GAMMER</player>
<system>SS</system>
<File>
My Method convert String to Xml:
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
Source src = null;
DocumentBuilder builder;
try {
builder = factory.newDocumentBuilder();
// Use String reader
Document document = builder.parse( new InputSource(new StringReader( xmlString ) ) );
src = new DOMSource( document );
FileWriter writer = new FileWriter(new File("./src/main/folder/xmlFile.xml"));
StreamResult result = new StreamResult(writer);
TransformerFactory transformerFactory = TransformerFactory.newInstance();
Transformer transformer = transformerFactory.newTransformer();
transformer.transform(src, result);
I Have method Marshaller:
JAXBContext context = JAXBContext.newInstance("br.com.Test.domain");
Marshaller m = context.createMarshaller();
Unmarshaller um = context.createUnmarshaller();
Object obj = um.unmarshal();
How to convert String to xml and using this method Marshaller?i don't understand how to pass a result of method conversion for method unmarshaller.
What kind should I go through? a Source?

DOM Parser not able to parse large xml file

I am trying to parse a large xml file using DOM Parser and Xpath, but it seems like my code breaks as it's a large xml file (60000 lines). When I try and print the xml, it starts printing from the middle of the xml. Any ideas how I can avoid this?
Regards
FileInputStream file = new FileInputStream(new File(filePath));
DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = builderFactory.newDocumentBuilder();
Document xmlDocument = builder.parse(file);
XPath xPath = XPathFactory.newInstance().newXPath();
disclaimer = xPath.compile(disclaimerPath + File.separator + "title").evaluate(xmlDocument);
TransformerFactory tf = TransformerFactory.newInstance();
Transformer transformer = tf.newTransformer();
transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
StringWriter writer = new StringWriter();
transformer.transform(new DOMSource(xmlDocument), new StreamResult(writer));
System.out.println(writer.getBuffer().toString().replaceAll("\n|\r", ""));

Editing xml content in java and passing it as string, using node preferably

I've a xml document, which will be used as a template
<?xml version="1.0" encoding="UTF-8" standalone="no"?><entry xmlns="http://www.w3.org/2005/Atom" xmlns:d="http://schemas.microsoft.com/ado/2007/08/dataservices" xmlns:m="http://schemas.microsoft.com/ado/2007/08/dataservices/metadata"><content type="application/xml"><m:properties><d:AccountEnabled>true</d:AccountEnabled><d:DisplayName>SampleAppTestj5</d:DisplayName><d:MailNickname>saTestj5</d:MailNickname><d:Password>Qwerty1234</d:Password><d:UserPrincipalName>saTestj5#identropy.us</d:UserPrincipalName></m:properties></content></entry>
I'm calling it in java using this code where payLoadXML.xml has the above content.
"InputStream is = getClass().getClassLoader().getResourceAsStream("/payLoadXML.xml");"
Now I'm trying to edit the tag values for example changing the from "saTestj5" to "saTestj6" and then converting this entire xml and storing it in xml. Can anyone tell me how can I achieve this? I was told this can be done by using "Node" is it possible?
Use jaxb or sax parsers convert into object by using getter method and change the object and convert back to xml
try this
DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder docBuilder = null;
docBuilder = docFactory.newDocumentBuilder();
Document doc = null;
InputStream is = getClass().getClassLoader().getResourceAsStream("/payLoadXML.xml");
doc = docBuilder.parse(is);
Node staff = doc.getElementsByTagName("m:properties").item(0);
Text givenNameValue = doc.createTextNode("abc");
Element givenName = doc.createElement("d:GivenName");
givenName.appendChild(givenNameValue);
staff.appendChild(givenName);
TransformerFactory transformerFactory = TransformerFactory.newInstance();
Transformer transformer = null;
transformer = transformerFactory.newTransformer();
DOMSource source = new DOMSource(doc);
StringWriter writer = new StringWriter();
StreamResult result = new StreamResult(writer);
transformer.transform(source, result);

How can I parse an XML file stored in blobstore on Google App Engine?

I need to parse an XML file stored in the blobstore. How do I do that?
This is what I have done till now:
FileService fileService = FileServiceFactory.getFileService();
AppEngineFile file = fileService.getBlobFile(new BlobKey(key));
I cannot parse the XML file using javax.xml package or can I? The classes in that package if put into action requires a reference of a java.io.File object. But I do not have that.
This is what I have been doing till now : (not on the server but locally)
File blobKeys = new File("/home/non-admin/NetBeansProjects/Personal Site_Testers/web/xml/xml_1.xml");
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
Document doc = db.parse(blobKeys);
Element root = doc.getDocumentElement();
Element firstName = doc.createElement("first-name");
firstName.setTextContent(name);
root.appendChild(firstName);
TransformerFactory tf = TransformerFactory.newInstance();
Transformer transformer = tf.newTransformer();
DOMSource source = new DOMSource(doc);
StreamResult result = new StreamResult(new File("/home/non-admin/NetBeansProjects/Personal Site_Testers/web/xml/xml_1.xml"));
transformer.transform(source, result);
Note : There are 3 xml files uploaded to the blobstore. Those xml files just have a root tag . I want to get the xml reference and parse them to append child node several times depending upon the request made.
If you want to parse *.xml, I think jdom2 may be is more convenient which I use in the app engine and it works.
However, you can read the FileService API document and there is an example with how to read and write the file. If you can get a InputStream or Reader, then you can use javax.xml to parse the xml content in the InputStream or Reader.
FileService fileService = FileServiceFactory.getFileService();
AppEngineFile readableFile = new AppEngineFile("your_file_name");
FileReadChannel readChannel = fileService.openReadChannel(readableFile,
false);
InputStream input = Channels.newInputStream(readChannel);
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
Document doc = db.parse(input);
Element root = doc.getDocumentElement();
Element firstName = doc.createElement("first-name");
firstName.setTextContent("content");
root.appendChild(firstName);
// Now write
GSFileOptionsBuilder optionsBuilder = new GSFileOptionsBuilder()
.setBucket("mybucket").setKey("myfile")
.setMimeType("text/html").setAcl("public_read")
.addUserMetadata("myfield1", "my field value"); // change as
// your need
AppEngineFile writableFile = fileService.createNewGSFile(optionsBuilder
.build());
FileWriteChannel writeChannel = fileService.openWriteChannel(
writableFile, false);
OutputStream output = Channels.newOutputStream(writeChannel);
TransformerFactory tf = TransformerFactory.newInstance();
Transformer transformer = tf.newTransformer();
DOMSource source = new DOMSource(doc);
transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
StreamResult result = new StreamResult(output);
transformer.transform(source, result);

Java XML parsing error : Content is not allowed in prolog

My code write a XML file with the LSSerializer class :
DOMImplementation impl = doc.getImplementation();
DOMImplementationLS implLS = (DOMImplementationLS) impl.getFeature("LS","3.0");
LSSerializer ser = implLS.createLSSerializer();
String str = ser.writeToString(doc);
System.out.println(str);
String file = racine+"/"+p.getNom()+".xml";
OutputStreamWriter out = new OutputStreamWriter(new FileOutputStream(file),"UTF-8");
out.write(str);
out.close();
The XML is well-formed, but when I parse it, I get an error.
Parse code :
File f = new File(racine+"/"+filename);
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
Document doc = builder.parse(f);
XPathFactory xpfactory = XPathFactory.newInstance();
XPath xp = xpfactory.newXPath();
String expression;
expression = "root/nom";
String nom = xp.evaluate(expression, doc);
The error :
[Fatal Error] Terray.xml:1:40: Content is not allowed in prolog.
9 août 2011 19:42:58 controller.MakaluController activatePatient
GRAVE: null
org.xml.sax.SAXParseException: Content is not allowed in prolog.
at com.sun.org.apache.xerces.internal.parsers.DOMParser.parse(DOMParser.java:249)
at com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderImpl.parse(DocumentBuilderImpl.java:284)
at javax.xml.parsers.DocumentBuilder.parse(DocumentBuilder.java:208)
at model.MakaluModel.setPatientActif(MakaluModel.java:147)
at controller.MakaluController.activatePatient(MakaluController.java:59)
at view.ListePatientsPanel.jButtonOKActionPerformed(ListePatientsPanel.java:92)
...
Now, with some research, I found that this error is dure to a "hidden" character at the very beginning of the XML.
In fact, I can fix the bug by creating a XML file manually.
But where is the error in the XML writing ? (When I try to println the string, there is no space before ths
Solution : change the serializer
I run the solution of UTF-16 encoding for a while, but it was not very stable.
So I found a new solution : change the serializer of the XML document, so that the encoding is coherent between the XML header and the file encoding. :
DOMSource domSource = new DOMSource(doc);
TransformerFactory tf = TransformerFactory.newInstance();
Transformer transformer = tf.newTransformer();
String file = racine+"/"+p.getNom()+".xml";
OutputStreamWriter out = new OutputStreamWriter(new FileOutputStream(file),"UTF-8");
transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
transformer.setOutputProperty(OutputKeys.INDENT,"yes");
transformer.transform(domSource, new StreamResult(out));
But where is the error in the XML writing ?
Looks like the error is not in the writing but the parsing. As you have already discovered there is a blank character at the beginning of the file, which causes the error in the parse call in your stach trace:
Document doc = builder.parse(f);
The reason you do not see the space when you print it out may be simply the encoding you are using. Try changing this line:
OutputStreamWriter out = new OutputStreamWriter(new FileOutputStream(file),"UTF-8");
to use 'UTF-16' or 'US-ASCII'
I think that it is probably linked to BOM (Byte Order Mark). See Wikipedia
You can verify with Notepad++ by example : Open your file and check the "Encoding" Menu to see if you're in "UTF8 without BOM" or "UTF8 with BOM".
Using UTF-16 is the way to go,
OutputStreamWriter out = new OutputStreamWriter(new FileOutputStream(fileName),"UTF-16");
This can read the file with no issues
Try this code:
InputStream is = new FileInputStream(file);
Document doc = builder.parse(is , "UTF-8");

Categories

Resources