Parsing an XML using XPath / DOM / SAX [closed] - java

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 8 years ago.
Improve this question
I have an xml that I need to Parse and fetch values from it. However , I am not sure which way of parsing would be the best for the xml of this type. I read about different ways but not sure if that's the best possible way. Could someone please help me write a java code to Parse this xml using the best approach ?
Thanks in advance !
Here's the XML :
<managementDomain>
<mtosi:additionalInfo>
<mtosi:nvs>
<stru:attributeName>Managed Device Name</stru:attributeName>
<stru:attributeValue>
<nonc:value>al-dcdc-numr-phe-eu</nonc:value>
</stru:attributeValue>
</mtosi:nvs>
<mtosi:nvs>
<stru:attributeName>NMDBF</stru:attributeName>
<stru:attributeValue>
<nonc:value>Y</nonc:value>
</stru:attributeValue>
</mtosi:nvs>
<mtosi:nvs>
<stru:attributeName>BFGCustrID</stru:attributeName>
<stru:attributeValue>
<nonc:value>3444</nonc:value>
</stru:attributeValue>
</mtosi:nvs>
<mtosi:nvs>
<stru:attributeName>BFGContractID</stru:attributeName>
<stru:attributeValue>
<nonc:value>12331</nonc:value>
</stru:attributeValue>
</mtosi:nvs>
</mtosi:additionalInfo>
<mtosi:mdVendorExtensions>
<mtosi:tmf854Version/>
<mtosi:extVersion/>
<mtosi:extAuthor/>
</mtosi:mdVendorExtensions>
<mtosi:managedElement>
<mtosi:manufacturer>
<nonc:ossValue>CISCO</nonc:ossValue>
</mtosi:manufacturer>
<mtosi:productName>
<nonc:value>CISCO2951</nonc:value>
</mtosi:productName>
<mtosi:meVendorExtensions>
<mtosi:tmf854Version/>
<mtosi:extVersion/>
<mtosi:extAuthor/>
<mtosi:managementIPAddress>
<mtosi:ipValue>
<nonc:value>10.32.22.49</nonc:value>
</mtosi:ipValue>
</mtosi:managementIPAddress>
</mtosi:meVendorExtensions>
</mtosi:managedElement>
</managementDomain>
I need to fetch :
ManagementIpAddress , BFGCustomerId , BFGContractID and Managed Device Name from this xml

Two possible ways of parsing this XML are DOM4J and SAX. The former is more memory intensive and loads the complete document into a Java object structure. With SAX you can parse the file by Streaming the content and "listening" for the Elements you want to extract.
So for your specific case - that is reading only some few elements - SAX might be the way to go.
The drawback of SAX is, that invites to some hackish solutions that only work with specific - and correct (i.e. in the best case pre-validated) XML files. You need to programm more carefully when using SAX.
(Of course with small XMLs it's not a shame to load it completly with DOM4J, if this is more convenient for you ;)

Related

Memory usage of StAX item reader and writer in Spring Batch [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 1 year ago.
Improve this question
I am using JAXB as a marshaller/unmarshaller to process xml in Spring Batch. How does StaxEventItemReader and StaxEventItemWriter consume memory when reading/writing from/to xml file? Does it store the whole xml size in memory before splitting it into chunks and still uses the same amount of memory throughout the step? Or does it read/write a fragment of the file based on chunk size then releasing the memory usage before processing the next chunk?
StaX and Sax both are event-based parser which parses through the XML line by line so it does not store any whole XML file within the memory. This can be an advantage if you have a large XML file and you want to traverse through it. However, if you want to access something in-between then it may not be useful as it does not store anything in memory.
You can also write your own custom context to store the XML elements and its children elements while parsing through the XML file using these libraries so as to store the particular chunk of XML file which you can use and later clear it out when you move to the next event of the XML.
So basically its very efficient as it does not store the whole XML file and it parses through the XML file line-by-line.
StaX means "Streaming API for XML". It parses the XML on-the-fly which means it doesn't hold the whole XML in memory. It is indeed very efficient memory-wise.
StaxEventItemReader class is based on pull-parsing. See the link for definition of what that means.

XML Document Parsing in Java [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
I need to parse an XML document in java for a web service I'm making, and save the contents of it.
I need to save the name of the tags, if the tag has attributes save the attributes, and then save the data within those tags. These three items will be inserted into a database table with the three columns tags, attributes, and data.
I'm using the following java libraries:
javax.xml.parsers.DocumentBuilder
javax.xml.parsers.DocumentBuilderFactory
org.w3c.dom.Document, org.w3c.dom.NodeList
org.xml.sax.InputSource.
Any help would be much appreciated.
DISCLAIMER: I don't want to plagiarize so I didn't include code but included links to other tutorials that are VERY helpful to this topic.
First, you should read w3c dom's java API because it tells you a lot of useful functions that are very related to your question.
Second, this website contains a useful tutorial that's easy to understand and it contains the necessary information for you to get the attributes of tags.
Third, this website gives you info on how to get tagName when you are looping through elements.
Fourth, you should always read related API, google, and then post a question if you are have no clue after a LONG period of time.
Lastly, you should post a difference question or research on database FIRST before asking that question here. This question should only be about XML Document Parsing in Java.
We are not supposed to help you do anything so the API is the best help for you (and google).
API: https://docs.oracle.com/javase/7/docs/api/org/w3c/dom/package-summary.html

best way for saving settings to external storage? [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 8 years ago.
Improve this question
In My app I want to have an external file for saving my settings, item tags , etc. (for using it in outside of app like sending it on another device and so on).
after researching a lot I came to have a text file like this:
settings.txt
"Item1" : "data1"
"Item2" : "data2"
...
now is it the best solution for my purpose?
and if it is, how am I supposed to get my data based on it's id (Item number) from this kind of text file? (I can do the writing part but have no idea how to retrieve it).
Thanks in Advanced.
Instead of your way, you can store the key and values in JSON format. That way it would be easier for you to parse the data and get the value based on the item number (key). You can even use Google's Gson lib o make things simpler. JSON.simple example – Read and write JSON will help as well.

Replacing XML Nodes in Java [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I have an XML document in a foreign language and another XML document in English. I am trying to replace some nodes in the foreign document with nodes from the English document and export the document.
I have been working on this for days now and have tried countless things form importing both documents into text with a Scanner, BufferedReader, etc. with no good results.
I'm at a loss on what else I can try. I have searched for days and have nothing. Maybe what I'm trying to do cannot be done although it seems simple enough. Any help/direction would be appreciated.
Put them into DOM objects, then use XPATH to locate and select nodes, to copy values between them.
Depending on what you need to replace and what you mean by "export", I would use an XML parser like SAX using the following algorithm
For each node that you read
Replace attributes or text as necessary
Write it out to the the a new XML file
There are many tutorials out there on how to use SAX, such as this one: How to parse XML using the SAX parser
If the "replacements" you need to do are very straightforward like "all <tag> objects under <parent-tag>" then maybe building the DOM and using XPath would work, but if your replacements are very arbitrary and unstructured then I'd go with parsers.

How to make large regex patterns in Java? [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 9 years ago.
Improve this question
I'm trying to parse(steal) a lot of information from a HTML page. And a lot of information is in blocks. like: username: 1.age 2.gender 3.country etc. It's a very large block and therefore my regex pattern is huge. All of my regex development tools have a single line for the pattern, and a textbox for the text. It makes developing these kind of large patterns impossible. What am I suppose to do to develop large regex patterns or do I avoid them?
HTML pages are basically a valid DOM strucure. So better use a DOM parser instead of regex to get the desired info. You can explore JSoup : Java HTML parser.
use the parsing rules described for HTML to generate the DOM trees from text/html resources. Together, these rules define what is referred to as the HTML parser.

Categories

Resources