Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to retrieve multiple attribute values from this XML file - using SimpleXML/Android

I have tried for many hours and have gone through all possible examples on Google, but I can't figure out how to get the attributes from the in this XML file using simplexml on Android.

XML File:

<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<Service>
<Facture FactNo="" 
 NoAppel="6" 
 ProjDate="2012-04-01"
 NomProj="MR. H. BLOOM"
 AddProj="20, GARDEN ROAD" 
 VilleProj="WESTMOUNT, QC."
 CPProj="" ContProj="M. BLOOM" 
 TelProj="(555) 555-9571" 
 SysMarque="" 
 SysMod="" 
 SysType="" 
 SysSerie="" 
 SysNo=""
 SysTemp=""
 Tension=""
 Phase="0"
 Halo="False"
 POFact=""
 NomFact=""
 AdrsFact="" 
 VilleFact=""
 CPFact=""
 TelFact=""
 FaxFact=""
 Trouble=""
 TbLock="False"
 TDesc=""
 TRec=""
 Depl="False"
 TM="False" 
 NoTM=""
 TPA="False"
 NoTPA=""
 CS="False"
 NoCS=""
 Estim="False"
 NoEstim=""
 Proj="False"
 NoProj=""
 Term="True" 
 Incompl="False"
 Garant="False"
 NoGarant=""
 Annexe="False"
 NoAnnexe=""
 NoContrat="00007"
 MainDoeuvre="0,0000" 
 Materiel="0,0000"
 SousTot=""
 TPS="0,0000"
 TPSTaux="0.05"
 TVQ="0,0000"
 TVQTaux="0.085"
 Total="0,0000" />
<Pieces>
<Piece Qty="10" Desc="test" PO="12345678" PrixUnit="0.0000" Montant="0.0000" />
<Piece Qty="25" Desc="testitem2" PO="33333" PrixUnit="22.0000" Montant="220.0000"            />
<Piece Qty="35" Desc="testitem3" PO="44444" PrixUnit="33.0000" Montant="440.0000"     />
</Pieces>
<Techs>
<Tech Nom="Bobby" Reg="1" TD="2" Taux="3.5000" Montant="0.0000" Lock="False" Date="2012-04-01   
10:49:00" />
</Techs>
<Sign />
<Pics />
</Service>

The XML file is third party so I cannot modify it.

Class for Service (only showing for Piece):

    //Liste Pieces
    @ElementList(name="Piece", inline=true)
    @Path("Pieces")
    private List<PieceXML> pieces;

    public List<PieceXML> getpieces()
    {
        return pieces;
    }       

Class for Piece:

@Root(name="Piece")
public class PieceXML 
{

@Attribute
private Double Qty;
@Attribute
private String Desc;
@Attribute
private String PO;
@Attribute
private Double PrixUnit;
@Attribute
private Double Montant;
 ...}

What I actually need is to build an array from the attribute values in Piece, but really can't figure out how to do it. Any help would be very appreciated. Thanks in advance.

like image 633
user1363718 Avatar asked Feb 22 '26 03:02

user1363718


1 Answers

OK finally got it working by myself. Figures out it was pretty straight-forward. I had it all the way but could not get it working because I had issues with nodes that were named exactly like existing classes of my application which resulted in getting null values when reading my returned list of objects. In that case I had to use the

@Path(pathname) 

call with complete path i.e.

@Path("Service/Pieces").  

I could then get my

List<PieceXML>

Object and fill my ArrayLists.

like image 123
user1363718 Avatar answered Feb 24 '26 16:02

user1363718



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!