• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Tim Cooke
  • Devaka Cooray
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
Bartenders:
  • Carey Brown
  • Roland Mueller

Search for a specific ID

 
Ranch Hand
Posts: 270
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have a XML document, where I want to make a look-up on a specific ID text ( see below - the infoID). How is the best way to find an element text in a big xml document. Lets say I want to find the infoID "ID-2"?

Should I just run through the entire XML document from the beginning and make a test, if the text is equal the ID I want to find or is there a better way?

// Run through every element
String infoID = e.getText();
if ( infoID.equals("ID-2") )
break;

<infoBlock>
<infoID>ID-1</infoID>
<infoSubject>My subject 1</infoSubject>
<infoText>My long text 1...</infoText>
</infoBlock>
<infoBlock>
<infoID>ID-2</infoID>
<infoSubject>My subject 2</infoSubject>
<infoText>My long text 2...</infoText>
</infoBlock>
[ January 17, 2006: Message edited by: Jeppe Fjord ]
 
Ranch Hand
Posts: 183
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
using XPath is a clean and flexible alternative
 
Jeppe Sommer
Ranch Hand
Posts: 270
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you.

I have been seaching around the web to find a simple example.

In my schema I have been given the infoID element an xpath field name. I have a certain unique infoID and want to find that infoID in the XML document. Then I want to parse the related nodes for that infoID, i.e. infoSubject, infoText.

//XSD for helpInfo
...
<xs:complexType>
<xs:sequence>
<xs:element name="infoBlock" maxOccurs="unbounded">
<xs:complexType>
<xs:sequence>
<xs:element name="infoID" type="infoID"/>
<xs:element name="infoRef" type="infoRef"/>
<xs:element name="infoSubject" type="infoSubject"/>
<xs:element name="infoText" type="infoText"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
...

<xs:unique name="infoID">
<xs:selector xpath=".//infoBlock"/>
<xs:field xpath="infoBlock"/>
</xs:unique>

Du you have any examples?
 
Hey, check out my mega multi devastator cannon. It's wicked. It makes this tiny ad look weak:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic