Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

Below is the XML:-

 <?xml version="1.0" encoding="utf-8"?>
<Stock>
    <Identification>
      <AccountID></AccountID>
      <CustomerId></CustomerId>
    </Identification>
 <Product>
    <ArticleName>Monitors</ArticleName>
    <BaseUnit></BaseUnit>
    <Notes></Notes>
    <ID>11f13e2e-ae97-45b5-a9a9-23fa7f6bb767</ID>
    <ID>b22834c0-a570-4e6b-97c3-5067a14d118d</ID>
    <ID>ed458593-5e1a-4dc1-94f0-a66eeef2dd79</ID>
    <ID>d25584a9-1db2-48cf-9a70-9b81e5a7e7f2</ID>
    <LogisticalInfo>
        <BaseUnit></BaseUnit>
        <Compoundheight>4.78</Compoundheight>
        <Compoundwidth>5.67</Compoundwidth>
        <Compounddepth></Compounddepth>
        <Compoundweight></Compoundweight>
        <CompoundweightUnit>g</CompoundweightUnit>
        <TotalHeight>30.5</TotalHeight>
        <Totalwidth>542.7</Totalwidth>
        <Totaldepth>37.5</Totaldepth>
        <TotalWeight>2840</TotalWeight>
        <height>mm</height>
        <Weight>g</Weight>
        <Depth>mm</Depth>
    </LogisticalInfo>
</Product>

I would like to extract Compoundheight and Compounddepth, Below is the part of code to extract it but it is throwing error:java.lang.StringIndexOutOfBoundsException.

 stringXmlDocument = productHeader + toStringXml(node, true) + productTrailer;

int CompoundheightCodeStart = stringXmlDocument.indexOf("<Compoundheight>");
int CompoundheightCodeEnd = stringXmlDocument.indexOf("</Compoundheight>"); height_packed=Double.parseDouble(stringXmlDocument.substring(CompoundheightCodeStart+13,CompoundheightCodeEnd));
int CompounddepthCodeStart = stringXmlDocument.indexOf("<Compounddepth>");
int CompounddepthCodeEnd = stringXmlDocument.indexOf("</Compounddepth>");depth_packed=Double.parseDouble(stringXmlDocument.substring(CompounddepthCodeStart+12, CompounddepthCodeEnd));
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
295 views
Welcome To Ask or Share your Answers For Others

1 Answer

A better approach than indexOf would be to use Java XPath implementation for navigating XML.

See the Java XPath implementation, and the XPath Specification.

An example:

    // parse the xml into a Document
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    DocumentBuilder builder = factory.newDocumentBuilder();
    InputStream inputStream = this.class.getResourceAsStream("test.xml");
    Document document = builder.parse(inputStream);

    // Obtain a specific element from within the Document
    XPathFactory xPathFactory = XPathFactory.newInstance();
    XPath xPath = xPathFactory.newXPath();
    String articleName = xPath.evaluate("/Stock/Product/ArticleName", document);
    System.out.println("ArticleName is: " + articleName);

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...