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
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
The image below describes what I want to do, so I'm supposed to add many values to this three tables.
I'm using the library docx4j
You can use content control databinding for this; docx4j's OpenDoPE convention allows you to repeat table rows. And more recent versions of Word have a concept of repeating content controls; see https://www.docx4java.org/blog/2015/01/word-2013-repeatingsection-content-controls-ready-for-prime-time/
In principle, docx4j supports both, but it'll be easier to get help with the OpenDoPE approach.
To get started, try invoice.docx from https://github.com/plutext/docx4j/tree/master/sample-docs/word/databinding which is an example of repeating table rows.
To merge invoice-data.xml (from the same dir) into it, use https://github.com/plutext/docx4j/blob/master/src/samples/docx4j/org/docx4j/samples/ContentControlsMergeXML.java
If you like this approach, you'll need to author your own input document; to do this, you can try the "friendly" Word AddIn at https://opendope.org/implementations.html
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 ;)
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 to do a search in a text file or a large string to check if the text contains a set of keywords (could be millions). If it contains the keywords I have to highlight whatever keywords got matched. What approach should be taken for this? Does lucene provide a solution for this?
You've tagged your question with Elasticsearch - if you're open to using ES I think Percolation with highlighting may fit what you need. You could register each keyword as a separate query with the percolator and then run each document or string thru it. It will return a list of the queries that matched. You can also combine it with highlighting.
http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/search-percolate.html
http://blog.qbox.io/elasticsesarch-percolator
You can use lucene ShingleFilter
You will find lots of example on the net, here is one http://www.massapi.com/class/sh/ShingleFilter.html
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 fairly new to android programming and have a couple years of university experience in Java. I am planning on writing an app for android that will require me to see what category an item entered belongs.
As an example, if a user inputs 'apple', that data must be placed under a 'fruit' category. So in other words I require a data file that will be able to tell me what category the item is in or alternatively the user can add an item to a category if it is not yet categorised.
I am wondering what would be the best way to store this data. Should I use an xml file, database file, text file or what? It would be necessary to perform look-ups and also insertions.
Thanks in advance
I think this question is more of a personal preference, but I would use XML in this case. You can have the structure predefined with distribution of your app, and modify/write the file at runtime. Its easy to read and understand, and just as easily modified. Simply universal.
Sqlite has its positives in large forms of data, but can be much more complex for something that doesn't need much detail.
<index>
<category name="fruit">
<item>apple</item>
.....
</category>
......
</index>
A simple for loop through the categories, and see if the item is present, else add it.
Hope this helps, happy coding!
Android has SQLite built in...for your purposes I'd go with that.
http://developer.android.com/guide/topics/data/data-storage.html
http://developer.android.com/training/basics/data-storage/index.html
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.