What is the best way to parse an xml...

User 2926449 Photo


Guest
1 post

I have an xml file with product data which I get from API as response. what is the best way to parse that xml file? Should I use server side technologies or any client side scripts? Any sample or code snippets available?
Thank you
User 122279 Photo


Senior Advisor
12,548 posts
Online Now

What about this: https://jsonformatter.org/xml-parser
Ha en riktig god dag!
Inger, Norway
My new honey site, built with RFF: https://eikweb.com/horgenhonning/
Component sharing for RFF, RBB, RSD and SD: https://eikweb.com/sharing/


User 2926954 Photo


Guest
1 post

You can easily parse your xml by using DOMParser with XMLHttpRequest object. You can refer this read xml with jquery as there is code sample with an example to parse xml. Let me know if it help or I can send you small sample which you can use easily for your solution.
Thank you
John
User 2929386 Photo


Guest
5 posts

I often use this "https://jsonformatter.org/xml-parser"
User 2932062 Photo


Guest
3 posts

If you have a concert data model that is generating the XML I would take a look at XStream
User 2930625 Photo


Guest
1 post

package for_so;

import java.io.File;
import java.util.ArrayList;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import rasmus_torkel.xml_basic.read.TagNode;
import rasmus_torkel.xml_basic.read.XmlReadOptions;
import rasmus_torkel.xml_basic.read.impl.XmlReader;

public class Q15626686_ReadOrder
{
public static class Order
{
public final Date _date;
public final int _customerId;
public final String _customerName;
public final ArrayList<Item> _itemAl;

public
Order(TagNode node)
{
_date = (Date)node.nextStringMappedFieldE("Date", Date.class);
_customerId = (int)node.nextIntFieldE("CustomerId");
_customerName = node.nextTextFieldE("CustomerName");
_itemAl = new ArrayList<Item>();
boolean finished = false;
while (!finished)
{
TagNode itemNode = node.nextChildN("Item");
if (itemNode != null)
{
Item item = new Item(itemNode);
_itemAl.add(item);
}
else
{
finished = true;
}
}
node.verifyNoMoreChildren();
}
}

public static final Pattern DATE_PATTERN = Pattern.compile("^(\\d\\d\\d\\d)\\/(\\d\\d)\\/(\\d\\d)$");

public static class Date
{
public final String _dateString;
public final int _year;
public final int _month;
public final int _day;

public
Date(String dateString)
{
_dateString = dateString;
Matcher matcher = DATE_PATTERN.matcher(dateString);
if (!matcher.matches())
{
throw new RuntimeException(dateString + " does not match pattern " + DATE_PATTERN.pattern());
}
_year = Integer.parseInt(matcher.group(1));
_month = Integer.parseInt(matcher.group(2));
_day = Integer.parseInt(matcher.group(3));
}
}

public static class Item
{
public final int _itemId;
public final String _itemName;
public final Quantity _quantity;

public
Item(TagNode node)
{
_itemId = node.nextIntFieldE("ItemId");
_itemName = node.nextTextFieldE("ItemName");
_quantity = new Quantity(node.nextChildE("Quantity"));
node.verifyNoMoreChildren();
}
}

public static class Quantity
{
public final int _unitSize;
public final int _unitQuantity;

public
Quantity(TagNode node)
{
_unitSize = node.attributeIntD("unit", 1);
_unitQuantity = node.onlyInt();
}
}

public static void
main(String[] args)
{
File xmlFile = new File(args[0]);
TagNode orderNode = XmlReader.xmlFileToRoot(xmlFile, "Order", XmlReadOptions.DEFAULT);
Order order = new Order(orderNode);
System.out.println("Read order for " + order._customerName + " which has " + order._itemAl.size() + " items");
}
}
You will notice that the retrieval functions end in N, E or D. They refer to what to do when the desired data item is not there. N stands for return Null, E stands for throw Exception and D stands for use Default.
User 2932528 Photo


Guest
1 post

I often use this "https://jsonformatter.org/xml-parser"

Edited by Forum Admin:
You can post your to-the-point replies, but if you continue posting spam URLs, you will be banned from the forum.
User 2932283 Photo


Guest
9 posts

Try this method for the best way to parse xml file. You need to read line by line and fetch information because xml parser converts an xml document into an xml DOM object.;)
User 2935107 Photo


Guest
2 posts

Hi there. Interesting thread, thanks for the information :)

Have something to add? We’d love to hear it!
You must have an account to participate. Please Sign In Here, then join the conversation.