java: Adding recursively to an xml - java

I need a help. how can i add content recursively to an xml file. i have a program which processes a file and send 'line information's. these line informations needs to be written to an XML file, like shown below. what i do now is I read each line info and then send it a fn which writes XML. I want to know if there is any way to Buffer the Document object and then keep keeping appending to that Document object when each new line comes.
<?xml version="1.0" encoding="UTF-8" standalone="no" ?>
- <Rev_1.28>
- <OP type="SAM">
<SRC_LN_FROM>612612</SRC_LN_FROM>
<SRC_LN_TO>703703</SRC_LN_TO>
<NO_LINES>92</NO_LINES>
</OP>
- <OP type="MOV">
<SRC_LN_FROM>6122</SRC_LN_FROM>
<SRC_LN_TO>7033</SRC_LN_TO>
<NO_LINES>9</NO_LINES>
</OP>
</Rev_1.28>

You can use DOM parser to create use org.w3c.dom.Document object as shown here.
The data is stored in primary memory, so this approach is acceptable if the data to be written is relativley small.

Related

Write stylesheet tag with XML API (STaX/DOM/..)

i'm having some trouble to write a particular xml tag (using an XmlStreamWriter).
Basically, we have an XMLWriter that is based on "javax.xml.stream.XMLStreamWriter" (STaX) which is working fine.
All the xml files that are written begin automatically with the tag :
< ?xml version="1.0" encoding="ISO-8859-1"?> (first space is added to display the xml line)
What we need now is to add a new line (stylesheet) to write every single xml file with the beginning lines :
< ?xml version="1.0" encoding="ISO-8859-1"?> (same as above)
< ?xml-stylesheet type="text/xsl" href="myXsl.xsl"?> (same as above)
I tried to do it the hard-coded way, using the XmlStreamWriter.writeCharacters(String) but the problem is that "<" and ">" are special characters so the output in the xml file is "<"/">".
Also, this is not very clean coding..
In the same way that STaX writes the first line using "XMLStreamWriter.writeStartDocument(String encoding, String version)", does anyone know an XML (XSL/XSLT?) API which WRITER does write the tag :
< ?xml-stylesheet type="text/xsl" href="myXsl.xsl"?> (same as above)
Any help would be much appreciated :)
It is called a processing instruction.
See XMLStreamWriter.writeProcessingInstruction, for instance.
In your case:
writer.writeProcessingInstruction("xml-stylesheet",
"type=\"text/xsl\" href=\"myXsl.xsl\"");
(Not tested.)

How can i modify xml-stylesheet attribute value in java

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<?xml-stylesheet href="Sample.xsl" type="text/xsl"?>
<MyDoc>.....</MyDoc>
I want to modify the attribute href's value to 'MyDoc.xsl'. I have tried using XPath but it returns nothing:
//xml-stylesheet[contains(text(), 'Sample.xsl')]/#href";
Also using Document only gives elements starting at MyDoc
NodeList list = taggedC32Doc.getElementsByTagName("*");
Is there any way i can do this?
The line you want to change is a Processing Instruction, not an Element, so neither of your attempts to find it as an element will work. Try
/processing-instruction(xml-stylesheet)
You can then get that node's data, which will be href="Sample.xsl" type="text/xsl". Perform the appropriate string manipulation to find and change the href pseudo-attribute in that string -- sorry, most XML APIs don't provide any assistance in doing so, because as far as XML is concerned the PI's data is an unformatted string even though it's usually structured to resemble attributes -- and set the new data back into the ProcessingInstruction node.

Transform xml with data from different sources to another xml using java

I have an xml file in which some elements contain certain values.For example:
<item>
<origin>
<![CDATA[KWI]]>
</origin>
<destination>
<![CDATA[DOH]]>
</destination>
</item>
I have an excel sheet containing the country codes and port code mapping as:
COUNTRY_CODE PORT_CODE MANAGING_PORT_STATION
KW KWI MPS1
QA DOH MPS2
In the output xml, i need to put it something like:
<itemOut>
<country><![CDATA[KW]]></country>
<managingPortStation>MPS1</managingPortStation>
<dest><![CDATA[DOH]]></dest>
</itemOut>
So in short, I need to combine some non xml sources into the output xml file based on the input xml file, along with the xml file.
To accomplish the above, what should I use? Is it possible via xslt? Or what API's are available with java. I have just skimmed through the jaxp. But is it worth spending more time for my case? I would prefer to do it with java,rather than xslt since I am more familiar with it.

how to parse 2 xml files in android

hi i am a new developer and i want know how to do parsing of two xml files in a project.
I have 2 xml files. the first one is as follows
<?xml version="1.0"?>
<X>
<Y>
<Z>
A
</Z>
<packs>
<pack>
<packname>B</packname>
</pack>
</packs>
</Y>
</X>
The next xml files looks as follows
<s>
<t>
<question>abc</question>
<question>def</question>
<question>ghi</question>
</t>
</s>
The first XML file works for me. When i touch A it moves over to B. Now when i touch B i want to show the first question alone ie abc, Can it be done, it is not working for me
pls tell me how to move from one xml file to the other xml file
can anyone explain this with a sample codes....
Where should i store the 2 files to be parsed... I have tried storing it in raw folder in Resources.
Here is an overview of some XML parsers available for Android including some examples.
It depends on your application needs where you want to store your xml file (xml from a web service call mey remain temporarily in memory). Files like xml should be generally stores in the Raw folder.
You can use SAX Parser or Pull Parser to parse the xml. Following are some links for help:
SAXParser
Example

Best approach to serialize XML to stream with Java?

We serialize/deserialize XML using XStream... and just got an OutOfMemory exception.
Firstly I don't understand why we're getting the error as we have 500MB allocated to the server.
Question is - what changes should we make to stay out of trouble? We want to ensure this implementation scales.
Currently we have ~60K objects, each ~50 bytes. We load the 60K POJO's in memory, and serialize them to a String which we send to a web service using HttpClient. When receiving, we get the entire String, then convert to POJO's. The XML/object hierarchy is like:
<root>
<meta>
<date>10/10/2009</date>
<type>abc</type>
</meta>
<data>
<field>x</field>
</data>
[thousands of <data>]
</root>
I gather the best approach is to not store the POJO's in memory and not write the contents to a single String. Instead we should write the individual <data> POJO's to a stream. XStream supports this but seems like the <meta> element wouldn't be supported. Data would need to be in form:
<root>
<data>
<field>x</field>
</data>
[thousands of <data>]
</root>
So what approach is easiest to stream the entire tree?
You definitely want to avoid serializing your POJOs into a humongous String and then writing that String out. Use the XStream APIs to serialize the POJOs directly to your OutputStream. I ran into the same situation earlier this year when I found that I was generating 200-300Mb XML documents and getting OutOfMemoryErrors. It was very easy to make the switch.
And ditto of course for the reading side. Don't read the XML into a String and ask XStream to deserialize from that String: deserialize directly from the InputStream.
You mention a second issue regarding not being able to serialize the <meta> element and the <data> elements. I don't think this is an XStream problem or limitation as I routinely serialize much more complex structures on the order of:
<myobject>
<item>foo</item>
<anotheritem>foo</anotheritem>
<alist>
<alistitem>
<value1>v1</value1>
<value2>v2</value2>
<value3>v3</value3>
...
</alistitem>
...
<alistitem>
<value1>v1</value1>
<value2>v2</value2>
<value3>v3</value3>
...
</alistitem>
</alist>
<anotherlist>
<anotherlistitem>
<valA>A</valA>
<valB>B</valB>
<valC>C</valC>
...
</anotherlistitem>
...
</anotherlist>
</myobject>
I've successfully serialized and deserialized nested lists too.
Not sure what the problem is here...you've found your answer on that webpage.
The example code on the link you provided suggests:
Writer someWriter = new FileWriter("filename.xml");
ObjectOutputStream out = xstream.createObjectOutputStream(someWriter, "root");
out.writeObject(dataObject);
// iterate over your objects...
out.close();
and for reading nearly identical but with Reader for Writer and Input for Output:
Reader someReader = new FileReader("filename.xml");
ObjectInputStream in = xstream.createObjectInputStream(someReader);
DataObject foo = (DataObject)in.readObject();
// do some stuff here while there's more objects...
in.close();
I'd suggest using tools like Visual VM or Eclipse Memory Analyzer to make sure you don't have a memory leak/problem.
Also, how do you know each object is 50 bytes? That doesn't sound likely.
Use XMLStreamWriter (or XStream) to serialize it, you can write whatever you want on it. If you have the option of getting the input stream instead of the entire string, use a SAXParser, it is event based and, although the implementation maybe a little bit clumsy, you will be able to read any XML that is thrown at you, even if it the XML is huge (I have parse 2GB+ more XML files with SAXParser).
Just as a side note, you should send the binary data and not the string to a XML parser. XML parsers will read the encoding of the byte array that is going to come next through the xml tag in the beginning of the XML sequence:
<?xml version="1.0" encoding="UTF-8" standalone="no" ?>
A string is encoded in something already. It's better practice to let the XML parse the original stream before you create a String with it.

Categories