Get XML node attribute value Java - java

I have an XML Document which contains XML elements containing attributes from which I would like to get it's value and store it in a Hashmap.
Example:
<?xml version="1.0" encoding="UTF-8"?>
<Nodes>
<Node name="test1">
<mou>
<line3>hello</line3>
</mou>
</Node>
<Node name="test2">
<mou>
<line3>hello</line3>
</mou>
</Node>
<InputNode name="Chance">
<Test>
<RoundTo>100</RoundTo>
</Test>
</InputNode>
<InputNode name="total" />
</Nodes>
I'd like to parse this xml and retrieve the values attributes from all the elements named 'Node' and store it in a map object. So from the example above I would get
[name=test1,name=test2]
The problem is with maps the keys must be unique. How can I accomplish my goal using Java?

Sorry, I got your question wrong. You can parse the xml already. You want to store it in a map. You can create objects out of all the nodes. Node { nodeName, attribute1, attribute2} then you can save it as a map by {key,value} = {name, nodeObj}

Related

XPath returns for one element but doesn't return the other?

I am using Java to extract values using XPath. I was able to extract elements under the element fields but the elements under records are not returned.
XML is as follows:
<?xml version="1.0" ?>
<qdbapi>
<action>****</action>
<errcode>0</errcode>
<errtext>No error</errtext>
<qid>****</qid>
<qname>****</qname>
<table>
<fields>
<field id="19" field_type="text" base_type="text">
</field>
</fields>
<records>
<record>
<f id="6">1</f>
</record>
</records>
</table>
</qdbapi>
Code below:
XMLDOMDocObj.selectNodes("//*[local-name()='fields']")//21 fields returned
XMLDOMDocObj.selectNodes("//*[local-name()='records']")//no records are returned
XML must have a single root element; yours has two: fields and records.
Wrap them in a single common root to get the results you expect.
Also, if your XML has no namespaces, there's no reason to defeat them. Instead of
//*[local-name()='records']
use
//records
See also
How does XPath deal with XML namespaces?
Why must XML documents have a single root element?
What is the difference between root node, root element and document element in XML?

How to get only first level nodes with Jsoup

I have following XML tree and I'm using Jsoup to parse it.
<?xml version="1.0" encoding="UTF-8" ?>
<nodes>
<node>
<name>NODE 1</name>
<value1>
<value1>NODE 1 VALUE 1</value1>
</value1>
<nodes>
<node>
<name>NODE 1 CHILD</name>
<value1>NODE 1 CHILD VALUE 1</value1>
</node>
</nodes>
</node>
<node>
<name>NODE 2</name>
<value1>NODE 2 VALUE 1</value1>
</node>
</nodes>
However when I try to get only first level of node-elements. It returns all elements including children nodes, and it is doing it correctly, because clearly child elements also match my query.
Elements elements = data.select("nodes > node");
Is there any way to get just first level node-elements without adding additional level information to XML data?
You can do something like this:
Elements elements = data.select("nodes").first().select("> node");
This will work as well:
Elements elements = data.select("> nodes > node");
but only if you've used Jsoup.parse(xml, "", Parser.xmlParser()) to parse the XML and the XML is indeed as you've specified in your question (<nodes> is the root element)

Creating an array-list out of an XML file

I'm trying to create an array list out of an XML file.
In fact, I need to read a user input(which would be one of the elements) and return a certain node value.Here's the XML file:
<?xml version="1.0" ?>
- <types>
- <type id="Nourriture" taxe="0.1">
<element>pomme</element>
<element>fraise</element>
<element>fromage</element>
<element>viande rouge</element>
</type>
- <type id="Matiere Premiere" taxe="0.2">
<element>fer</element>
<element>polypropylene</element>
</type>
- <type id="Element Solide" taxe="0.3">
<element>voiture</element>
<element>planche surf</element>
<element>pistolet</element>
</type>
</types>
What I'm asked for is to see element, then depending on that, I need to return the "taxe" value.
I literally tried most things to do that, until I noticed my only solution is putting them inside an array /array list as follows:
`ArrayList arraylistobject = new ArrayList();
arraylistobject.add(....);`
etc...
Any thoughts on how to do this?
One obvious way is to use XPath:
//element[text()="fer"]/parent::type/string(#taxe)
(search for element whose text is "fer", select it's parent type, get string value of the parent's taxe attribute)

Remove XML Node when Freemarker Variable defined inside that node is not used or set

My XML has Freemarker Variables ${xyz} defined in it as show in the below example. After processing this XML against a Map I replaced Freemarker Variables with relevant values.
I would like to delete those XML nodes whose freemarker variables are not used.
Example Scenario:
Below XML having Freemarker variables ${xyz}. I also have empty nodes in them example ShouldNotDelete node.
Freemarker Template:
<property>
<Address>
<Organisation>${Organisation?c}</Organisation>
<BuildingName>${BuildingName?c}</BuildingName>
<Town>${Town?c}</Town>
<PostCode>${PostCode?c}</PostCode>
</Address>
<ShouldNotDelete></ShouldNotDelete>
</property>
I have a Map
Map { “Organisation” : “abc” , “PostCode” : “ME165AB” }
Both Town and Building Name are missing in this Map.
After processing Map and putting relevant values into above XML I get the below Result XML:
Result XML:
<property>
<Address>
<Organisation>abc</Organisation>
<BuildingName></BuildingName>
<Town></Town>
<PostCode>ME165AB</PostCode>
</Address>
<ShouldNotDelete></ShouldNotDelete>
</property>
I would like the unused freemarker nodes to be deleted. In the above case Town and BuildingName nodes should be deleted. Hence the Ideal output needed is below:
Ideal Result Output needed:
<property>
<Address>
<Organisation>abc</Organisation>
<PostCode>ME165AB</PostCode>
</Address>
<ShouldNotDelete></ShouldNotDelete>
</property>
Could anyone please suggest an ideal answer either by using Freemarker template language (like using <#if > <#else> before nodes) or by using Java logic.
You can try this :
<property>
<Address>
<#if Organisation??>
<Organisation>${Organisation?c}</Organisation>
</#if>
</Address>
<ShouldNotDelete></ShouldNotDelete>
</property>

How to add multiple attribute values to xml file in JAVA using DOM

I have One XML Like
<root>
<name id="1">Abc</name>
<salary>25000</salary>
</root>
I want something like this
<root>
<name id="1,2">Abc</name>
<salary>25000</salary>
</root>
I am able to create the attribute by using DOM parser as:
Document doc = _docBuilder.newDocument();`
Attr attr = doc.createAttribute("id");
attr.setValue("1");
name.setAttributeNode(attr);
How can I get multiple attribute values for the same attribute.
XML does not support attributes with multiple values.
You could certainly do: attr.setValue("1,2");
However that really isn't very XML friendly. Also, you probably shouldn't have more than one value for an id. You may wish to consider something like this:
<thing>
<name>Abc</name>
<reference_ids>
<id>1</id>
<id>2</id>
</reference_ids>
</thing>

Categories

Resources