1

I just started to try Jaxp13XPathTemplate but I'm a bit confused on parsing the XML.

Here is the sample XML

<fxDataSets> 
<fxDataSet name="NAME_A">
  <link rel="self" href="http://localhost:8080/linkA"/>
  <baseCurrency>EUR</baseCurrency>
  <description>TEST DESCRIPTION A</description>
</fxDataSet>

<fxDataSet name="NAME_B">
  <link rel="self" href="http://localhost:8080/linkB"/>
  <baseCurrency>EUR</baseCurrency>
  <description>TEST DESCRIPTION B</description>
</fxDataSet>
<fxDataSets>  

I'm already able to get NAME_A and NAME_B however I'm not able to get the description for both Node.

Here is what I have come up with.

XPathOperations  xpathTemplate = new Jaxp13XPathTemplate();
    String fxRateURL = "http://localhost:8080/rate/datasets";
    RestTemplate restTemplate = new RestTemplate();
    Source fxRate = restTemplate.getForObject(fxRateURL,Source.class);
    List<Map<String, Object>> currencyList = xpathTemplate.evaluate("//fxDataSet", fxRate , new NodeMapper() {
        public Object mapNode(Node node, int i) throws DOMException 
        {
            Map<String, Object> singleFXMap = new HashMap<String, Object>();
            Element fxDataSet = (Element) node;
            String id    = fxDataSet.getAttribute("name");

            /* This part is not working
            if(fxDataSet.hasChildNodes())
            {
                NodeList nodeList = fxDataSet.getChildNodes();
                int length = nodeList.getLength();

                for(int index=0;i<length;i++)
                {
                    Node childNode = nodeList.item(index);
                    System.out.println("childNode name"+childNode.getLocalName()+":"+childNode.getNodeValue());
                }

            }*/

            return new Object();
        }
    });

3 Answers 3

1

try to use dom4j library and it's saxReader.

    InputStream is = FileUtils.class.getResourceAsStream("file.xml");

    SAXReader reader = new SAXReader();
    org.dom4j.Document doc = reader.read(is);
    is.close();
    Element content = doc.getRootElement();  //this will return the root element in your xml file
    List<Element> methodEls = content.elements("element"); // this will retun List of all Elements with name "element" 
Sign up to request clarification or add additional context in comments.

1 Comment

hi Ademiban, I had got the Element already. The problem here is how to get description tag value, not to get the element.
1

Take a look public <T> List<T> evaluate(String expression, Source context, NodeMapper<T> nodeMapper)

  • evaluate takes NodeMapper<T> as one of its parameter
  • it returns object of type List<T>

But for your given code snippet:

  • its passing new NodeMapper() as parameter
  • but trying to return List<Map<String, Object>> which is surely violation of the contract of the api.

Probable solution:

I am assuming you wanna return a object of type FxDataSet which wraps <fxDataSet>...</fxDataSet> element. If this is the case,

  • pass parameter as new NodeMapper<FxDataSet>() as parameter
  • use List<FxDataSet> currencyList = ... as left hand side expression;
  • change method return type as public FxDataSet mapNode(Node node, int i) throws DOMException.

Take a look at the documentation also for NodeMapper.

Surely, I have not used Jaxp13XPathTemplate, but this should be common Java concept which helped me to find out what was wrong actually. I wish this solution will work.

Comments

0

If you want to get at the child nodes of the fxDataSet element you should be able to do:

Node descriptionNode= fxDataSet.getElementsByTagName("description").item(0);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.