CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Oct 2003
    Posts
    20

    How do you continue parsing even when expected elements are missing from xml file

    Hi,

    I have an XML file that can have a varying structure. For example in the chunk below the <subtrade> element has a subelement called <creditCurveFull>. However, <creditCurveFull> may be missing from the next <subtrade> element and reappear in the next <subtrade> element and so on.

    Currently, my code just bombs out if it does not see the element that it is expecting to parse i.e. <creditCurveFull>. How do I deal with this so that my code continues to parse the rest of the document even if the elements that it is expecting to see are missing?

    code is attached.

    xml chunk
    -----------

    <book>
    <riskGroup>GCPIG</riskGroup>
    <summitBook>GCPIG</summitBook>
    <desk>GCP_LN</desk>
    - <counterparty>
    <cptyId>DB-LN-CDTLEM</cptyId>
    <cptyIdLong>Deutsche Bank AG, London</cptyIdLong>
    - <trade>
    <tradeId>C414287M</tradeId>
    - <subtrade>
    <primitiveTrade>1</primitiveTrade>
    <analyticsProductType>DEFAULTSWAP</analyticsProductType>
    <assetClass>BUCOMK</assetClass>
    <ccy>USD</ccy>
    <maturityDate>2004-03-19</maturityDate>
    <effectiveDate>2003-03-19</effectiveDate>
    <tradeDate>2003-03-19</tradeDate>
    <notional>2000000.00</notional>
    <notionalCurrency>USD</notionalCurrency>
    <creditCurveFull>BUMIPUTRA COMMERCE BANK BHD</creditCurveFull>
    <countryOfIssuer>Malaysia</countryOfIssuer>
    <sector>BANKING</sector>
    <buySell>S</buySell>
    <interestRate>0.0065</interestRate>
    <productType>DEFSWAP</productType>
    <payFreq>Quarterly</payFreq>
    - <reportcollection>
    - <report>
    <reportName>PRICE_CARRY_TODAY</reportName>
    - <riskPoint>
    <value>-4952.002523</value>
    </riskPoint>
    </report>
    </reportcollection>
    </subtrade>
    </trade>
    </counterparty>
    Attached Files Attached Files

  2. #2
    Join Date
    May 2003
    Location
    Denmark
    Posts
    1,315
    Post an example that acctually runs, and I'll have a look at the problem.

    But what are you trying to do ?, if all you do is transform an xml document to plain text, then it would be much easier to use xslt.

  3. #3
    Join Date
    Oct 2003
    Posts
    20
    I have tried attaching actual file(s) but the forum will not allow me. If you send me your email address I will post you teh code and the xml file.

  4. #4
    Join Date
    May 2003
    Location
    Denmark
    Posts
    1,315
    Put them in a zip file and attach that.

  5. #5
    Join Date
    Dec 2002
    Posts
    47
    hi zeee;

    First, the text file that you posted shows code for walking the DOM tree so apparently the XML data has already been parsed and loaded into the DOM tree.

    Just have your code check for the presence of the optional child element. If it's there then process it. If not then move on.

    You may also want to organize your code so that each XML element produces a Java object.

    In pseudo-code, something like ...

    Code:
    if (element.name == "book")
    {
      Book b = new book(element);
      container.add(b);
    }
    In this example, the constructor initializes itself from the XML element. Any child elements would be handled inside the constructor which would do nothing more than pass the child element to another constructor ...

    Code:
    class Book
    {
    Book(Element element)
    {
      // grab attributes, store values.
    
      // iterate child elements.
    
      if ( child.name == "chapter" )
      {
        Chapter chap = new Chapter(child);
        chapters.add(chap);
      }
    }
    
    // and so on.
    -rick
    Last edited by rfmobile; October 14th, 2003 at 09:04 AM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured